Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

DayLight saving problem  RSS feed

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, my system is in PDT

This is my code :



Output is :


t : 2012-04-03 01:00:00.0
t1 : 2012-04-03 01:00:00.0



The output is correct till 10-March-2012. It gains 1 hr after 11 March.
What should i do. I need to convert XMLGregorianCalendar to Timestamp.
My system time is in PDT.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
converLocalTimeToUtcTime is wrong. Not its implementation, but its existence. You don't need to (in fact, should not) convert the millis from one TZ to another. The millis (or the Date object that wraps them) represents an instant in time, without regard to TZ. If you're in PDT and I'm in EDT and my friend is in the part of Indiana that doesn't use DST so he's in EST, and we all do System.currentTimeMillis() or new Date() at the same instant, we'll all get the same millis value. There's no "converting" that Date or millis to a different TZ because it doesn't have a TZ.

The TZ only comes into play when you display the time. We can choose to display that instant in time as 5:00 PDT or 7:00 EST or 8:00 EDT. If you want to display your time as, say, 8:00 EDT instead of 5:00 PDT, then you use a SimpleDateFormat object and set its TZ appropriately, and then call format().
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would need to tell us when daylight saving time starts where you are. If it was 11th March, then isn’t your output correct?
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to do lots of date/time related operations, you might want to check out the JodaTime library. Not that it would save you from understanding time zones and daylight saving changes if you need to process them (eg. what Jeff said), but it provides more clear and consistent API to do date/time computations. And a good documentation too.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!