Does date d = Calendar.getTime () return the wrong time?

        Calendar calendar = Calendar.getInstance();
    calendar.set(calendar.HOUR, 8);
    calendar.set(calendar.MINUTE, 45);
    calendar.set(calendar.SECOND, 00);

    Date d = calendar.getTime();

    System.out.println(d);

Exit: Wed Oct 05 20:45:00 BST 2011

Can someone help me why this is?

+5
source share
2 answers

Calendar.HOUR Used to set the hour set at 12-hour time.

In other words, you will set Calendarto 20:45. It's 8:45 PM .

Use Calendar.HOUR_OF_DAYto set the 24 hour time value.

+9
source

Of course you install Calendar.HOUR, which represents a 1-12 hour half day. You should use HOUR_OF_DAY, which is a value of 0-23 for the whole day:

calendar.set(Calendar.HOUR_OF_DAY, 8);

Joda Time, API /:)

, , ... , someOtherThread.sleep(...), ...

+9

All Articles