Category Archives: apache-commons-dateutils

DateUtils#truncate() to work in UTC timezone

I have a java.util.Date on which i am using org.apache.commons.lang.time#truncate(javaUtilDateObj, Calendar.DATE) to convert the date to midnight.

The problem is the UTC date gets represented in local timezone and the truncate method just converts the Hour,minute and seconds to 00:00:00 without considering the timezone.

Example :- Say UTC time is 30 seconds from epoch. Wed Dec 31 19:00:30 EST 1969 is the Date object representation I am seeing. On calling DateUtils#truncate() method on the above date, the output is Wed Dec 31 00:00:00 EST 1969.

What i am expecting is if UTC time is 30 seconds from epoch. And if that can be represented/converted to Thu Jan 1 00:00:30 EST 1970, i can call DateUtils#truncate() method on that and expect Thu Jan 1 00:00:00 EST 1970.

Note: I am not in a position to use joda-time API and hence I am stuck with what i have.