Due to some clueless soul, we have several fields in a legacy database that uses a 6-digit integer to represent a date in (YYYYmm) format. The date field is assumed to be the last day of the given month.
I have written the following method to perform the conversion from int to java.util.Date
:
public Date convertIntToDate(Integer intDate) {
if (intDate < 100000 || intDate > 999999) {
log.warn("Unable to parse int date {}", intDate);
return null;
}
int intYear = intDate/100;
int intMonth = intDate - (intYear * 100);
Calendar result = new GregorianCalendar();
result.set(intYear, intMonth - 1, 1, 0, 0, 0);
return result.getTime();
}
It seems to work, but if anyone has any suggested improvements, I'd love to hear them.
intMonth
, use the%
(modulus) operator. (2) Ditch Hungarian notation. \$\endgroup\$