File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Calendar and UTC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calendar and UTC" Watch "Calendar and UTC" New topic

Calendar and UTC

Jason Blackmore

Joined: Apr 26, 2005
Posts: 3
I'm trying to get a time in UTC milliseconds using the following code:

Calendar nowCal = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
System.out.println("UTC=" + nowCal.getTimeInMillis());

However, the milliseconds returned are for BST - that is they are adjusted for DST. Is this a bug in Calendar or am I missing something simple? I have tried various things like passing in the locale etc. Do I need to switch off the option in windows for adjusting for DST?

Thanks in advance for any help.
Jason Blackmore

Joined: Apr 26, 2005
Posts: 3
Further Information...

If I toString the Calendar I get the following:

java.util.GregorianCalendar[time=1118139170796,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=UTC,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2005,MONTH=5,WEEK_OF_YEAR=23,WEEK_OF_MONTH=2,DAY_OF_MONTH=7,DAY_OF_YEAR=158,DAY_OF_WEEK=3,DAY_OF_WEEK_ IN_MONTH=1,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=12,SECOND=50,MILLISECOND=796,ZONE_OFFSET=0,DST_OFFSET=0]

useDaylight=false indicates that the calendar isnt adjusting for DST. The time is also correct - that is 1 hour behind British Summer Time.
I agree. Here's the link:
subject: Calendar and UTC