Search code examples
javaapache-commons

which timezone is used by DateUtils?


I'm trying to use DateUtils from apache-commons3, but can't understand which timezone it relies on:

Date date = DateUtils.truncate(date, Calendar.DATE);

How does it know which timezone I'm in?


Solution

  • The timezone which is the default of your computer. Looking at the source code, it does this:

        public static Date truncate(Date date, int field) {
            if (date == null) {
                throw new IllegalArgumentException("The date must not be null");
            }
            Calendar gval = Calendar.getInstance();
            gval.setTime(date);
            modify(gval, field, MODIFY_TRUNCATE);
            return gval.getTime();
        }
    

    The documentation of Calendar.getInstance() says: Gets a calendar using the default time zone and locale.

    If you're willing to switch to JodaTime instead, here's a way do the same thing in JodaTime: JodaTime equivalent of DateUtils.truncate()