Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 873
yegor256 Avatar asked Dec 06 '25 04:12

yegor256


1 Answers

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()

like image 67
Daniel Kaplan Avatar answered Dec 07 '25 19:12

Daniel Kaplan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!