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

Wrong Hour/Timezone

 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this has been beat to death, and I tried to search first, but I can't seem to figure out way I get the wrong hour printed out such that it's 8 hours later than my current local time and into the next day respectively:




<code> ----------- CONSOLE OUTPUT --------------
nico@toshiba:~/progs/checkouts/java/java_progs/sierra_self_tests/chapter6$ java dates
Mon Jan 14 00:57:27 GMT 2008
Mon Jan 14 00:57:27 GMT 2008

c.toString(): java.util.GregorianCalendar[time=1200272247544,areFieldsSet=true,areAllFieldsSet=true,lenient=true,
zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],
firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2008,MONTH=0,WEEK_OF_YEAR=3,WEEK_OF_MONTH=3,
DAY_OF_MONTH=14,DAY_OF_YEAR=14,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,
HOUR_OF_DAY=0,MINUTE=57,SECOND=27,MILLISECOND=544,ZONE_OFFSET=0,DST_OFFSET=0]
after c.add(Calendar.HOUR, 2) as date: Mon Jan 14 02:57:27 GMT 2008
after c.add(Calendar.HOUR, -5) as date: Sun Jan 13 21:57:27 GMT 2008
</code>
And then right after I use the date shell command:
<code>
nico@toshiba:~/progs/checkouts/java/java_progs/sierra_self_tests/chapter6$ date
Sun Jan 13 16:57:38 PST 2008

</code>



[ broke up really long line that was messing up the page formatting - Jim ]
[ January 13, 2008: Message edited by: Jim Yingst ]
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, looks like edit has been disabled? Sorry about the code flub.

I forgot to mention that I'm on Linux-Kubuntu Gutsy
uname -a
Linux toshiba 2.6.20-16-generic #2 SMP Sun Sep 23 19:50:39 UTC 2007 i686 GNU/Linux
jdk: 1.6.0_03
 
Jeff Rummings
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output seems fine to me. Both dates are the same, except presented in different time zones. The date command you ran through the shell printed "Jan 13 16:57:38 PST" whereas the java output was "Jan 14 00:57:27 GMT". Both are equivalent, since the difference between the two time zones is 8 hours (or 7 hours during summer, when daylight savings are used).

By default JVM uses the time zone of the host OS. In your case this should be PST but obviously it's not. You can look into why this is the case, or otherwise you can use a date format to present the date in whatever time zone you want.

Hope this helps,

Arman
 
Jeff Rummings
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way you should probably split the calendar output into multiple lines so the posts are easier to read.
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for pointing this out Arman! So I'll still have to figure out why the default is off, but per your advice I've been able to manually set date/time for current time. Question: For a database driven application that needs timestamps, is it common to use both millis from epoch for the timestamp AND a human readable String for dates? I'm seeing more than one way to skin this cat so to speak. I'm starting on an app that needs to do a lot of timestamps, and read/write from records in database. Thanks again. Here's my new code:



And the output hopefully formatted better than last time:



This all looks like what I want and expected...thanks again Arman. It seems to me that the millis would be good for timestamp and the DataFormat.getTimeInstance is what I should use for presenting to user. Any other suggestions or things you think I should consider?
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gosh, I don't know what's going on with the formatting? On my browser the second post onward is way to the right and the green smileys got all over the code...I'm reposting:

-----------------

Thanks so much for pointing this out Arman! So I'll still have to figure out why the default is off, but per your advice I've been able to manually set date/time for current time. Question: For a database driven application that needs timestamps, is it common to use both millis from epoch for the timestamp AND a human readable String for dates? I'm seeing more than one way to skin this cat so to speak. I'm starting on an app that needs to do a lot of timestamps, and read/write from records in database. Thanks again. Here's my new code:



And the output hopefully formatted better than last time:



This all looks like what I want and expected...thanks again Arman. It seems to me that the millis would be good for timestamp and the DataFormat.getTimeInstance is what I should use for presenting to user. Any other suggestions or things you think I should consider?
 
Jeff Rummings
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: For a database driven application that needs timestamps, is it common to use both millis from epoch for the timestamp AND a human readable String for dates?


I'm not sure what you mean here. Are you asking whether to store millis vs strings in a database?

I would suggest storing values as java.sql.Timestamp objects since it represents an actual database data type. You can use Timestamp objects with PreparedStatements and ResultSets.

There's also a java.sql.Date class, however it only stores date information (year, month, date) but not time.

Btw, all implementations of Dates and Calendars in java are based on timestamp values expressed in milliseconds. I wouldn't suggest storing the actual millis in the database though.
 
Jeff Rummings
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to mention, if you disable smileys in your post it will get rid off them in the code
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the info Arman, you've been a great help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic