I am simply creating a java.util.GregorianCalender
GregorianCalendar calender = new GregorianCalendar();
now setting the hour,min,sec and ms as follows
c.set(Calendar.HOUR, 6);
c.set(Calendar.MINUTE, 45);
c.set(Calendar.SECOND, 30);
c.set(Calendar.MILLISECOND, 345);
now printing the output using getTime() method
System.out.println("Date and Time: "+c.getTime());
When System time is in AM, the output is
Date and Time: Tue Feb 09 06:45:23 IST 2016
And When System time is in PM, the output is
Date and Time: Tue Feb 09 18:45:23 IST 2016
Now my question is why the output is changing with the system time?
new GregorianCalendar()
fields are set using the current time in your default time zone. The AM/PM flag is therefore set depending on whether the current time in your default time zone is AM or PM.
When you set HOUR
to 6, the AM/PM flag is not changed and the resulting calendar is set on 6am or 6pm depending on the current time.
If you set HOUR_OF_DAY
to 6, the calendar will be set on 6am, regardless of current time.