Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar and UTC

 
Jason Blackmore
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic