• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Date problem

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In our application ,we have decided to all date in EST timezone .So in order to accomplish here is what we do . Utility method with timeZone offset as "America/New_York":

Here is how I am calling this method

when I run this code on a server deployed on INDIA (IST) : it stores the date in DB with local time instead of the time of the set timezone.
What am I doing wrong? How could I get a Date with values set to the preferred timezone?
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A java.util.Date object contains only the number of milliseconds since 1st Jan 1970 00:00:00 UTC (denoted the epoch). A java.util.Date does not and cannot contain anything about an explicit timezone. Normally databases follow the same approach and if yours does not then I would ask why not?
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Date has no idea about timezones. A Date just represents a point in time, this point in time is the same regardless of where in the world you are.

If you want to store the Date as a readable String with a timezone rather than a time stamp, then you should use SimpleDateFormat, set the timezone on it and format the Date object.

Welcome to JavaRanch!
 
Sarat M Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like a problem with Calendar.getInstance(TimeZone) method

As per doc for Calendar.getInstance(TimeZone) here is what it says :

public static Calendar getInstance(TimeZone zone)

Gets a calendar using the specified time zone and default locale. The Calendar returned is based on the current time in the given time zone with the default locale.

But it always returns the current local time
Here is what i tried :

Which return the same value of current time in milliseconds ( localtime) ; but this should return current time in "America/New_York" timezone right ?

Please correct my understanding
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's already been explained. Changing timezone does not change the value of the milliseconds, which always represents UTC ("Zulu time" for us old-timers).

Timezone is only relevant for display and for Calendars.
 
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[I'm replying to Sarat, without seeing Bear's response]:

No, that will print a number of milliseconds for the current time, independent of any timezone.
 
Sarat M Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Doc for Calendar is confusing

public static Calendar getInstance(TimeZone zone)
Gets a calendar using the specified time zone and default locale. The Calendar returned is based on the current time in the given time zone with the default locale.

Calendar .getInstance(TimeZone) do not compute/recalculate the timeInmills based on the timeZone ?


In short , I need a Date object which represents the Current Time in Newyork , so that I can save same date object in DB .How can achieve that ?
 
Mike Simmons
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget the Calendar and TimeZone, they are irrelevant.
 
Mike Simmons
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To save the date in DB, are you using JDBC? Hibernate? Something else? Anyway, as long as you use methods that treat the date as a java.util.Date, not a String, you should be fine. If at any time you convert the Date into a String, that is when you need to worry about time zones.
 
Sarat M Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your replies ..
My application is running on my localserver@India ( with hibernate as ORM and Oracle as DB) . Here is how I am setting date "u.setCreateDate(getAppDate());" Refer Thread 1 in the post .
Date saved in DB is showing as local time of Inida: 09 JAN ,2013 23:55 PM

Now I have Batch Job running on a server at 'NewYork' which also points against the same DB , and this batch job is configured to pick records which are created in last 10 mins
Current time in Newyork is 1:30 PM , so the above record is not picked up for processing .

My thought was to storing a Date object with newyork timings , but I am unable to achieve it
 
Mike Simmons
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are getAppDate() and setCreateDate() using Date objects, or Strings?

How does the batch job look for things in the last 10 min? How do you tell it to look for "10 minutes ago"?
 
Mike Simmons
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarat M Kumar wrote:Thanks a lot for your replies ..
My application is running on my localserver@India ( with hibernate as ORM and Oracle as DB) . Here is how I am setting date "u.setCreateDate(getAppDate());" Refer Thread 1 in the post .
Date saved in DB is showing as local time of Inida: 09 JAN ,2013 23:55 PM


For what it's worth, this sounds fine. That was the correct time for "now" as of a few minutes before your post, right? It's stored on the server correctly.
 
Sarat M Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getAppDate() and setCreateDate() uses Date Objects

Batch job logic : select * from USER where CREATE_DATE BETWEEN (sysdate-10/1440) AND sysdate;
sysdate-10/1440 : returns 10 minutes before current sysdate of DB .

DB server is running at "Newyork" with EST timezone
 
Sarat M Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I got a reference to create a DATE object with current "Newyork" time . ( Refer : http://techinitiatives.blogspot.com/2007/07/getting-java-date-in-another-timezone.html )
I just followed the above link and giving me desired results .But I want to understanding how it is working , Here is the code :


Output for the above code:
Getting Time in the timezone=America/New_York
Current Time there=Wed Jan 09 13:59:06 IST 2013 [ Displays the current time at 'Newyork']
Current Time there= Thu Jan 10 00:29:06 IST 2013 [Still shows local time at India ]

as I know , Date.toString return default timeZone ,so I can understand the reason for print IST ..
What made calendar to do the re-computation in 1st case ?Typically getInstance(timezone) also uses the same "GregorianCalendar" implementation only ,so why did fetch "newyork" timings


 
Mike Simmons
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Once again, this is a response to your previous post, not the one posted just one minute before this one]:

Oh, if the DB is in New York, then it should be using New York time internally, I see. However, it may still be correct, if there is a bug elsewhere in how you are looking at the data. How are you seeing that "Date saved in DB is showing as local time of Inida: 09 JAN ,2013 23:55 PM"? Are you doing a query through Java? SQLPlus, Or something else?

Are you using new Date() as I suggested? Or are you still messing around with Calendar? Calendar often just causes extra confusion in these cases.

 
Mike Simmons
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarat M Kumar wrote:Current Time there=Wed Jan 09 13:59:06 IST 2013 [ Displays the current time at 'Newyork']


Then why is it labeled as IST? This seems extremely backwards. I have the feeling it "works" from your perspective because there are two wrongs making a right. The getDateInTimeZone() method is one of them. If this works for now, great. But in the longer run, the goal should be to find the real problem and remove it, not resort to hacks like this.
 
Sarat M Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Queried by Toad ( Oracle SQL client)
Here is what I see :
1/9/2013 8:52:07 PM [ IST] This entry was inserted by server running at INDIA
1/9/2013 10:24:53 AM [EST ] This entry was inserted by server running at NEWYORK

Both the records are inserted at same time ..

Queried by Java (hibernate) client:

Output :
2013-01-09 20:52:07.0
2013-01-09 10:24:53.0
Current time --> Jan 9, 2013 10:22:07 AM
Current time --> Jan 8, 2013 11:54:53 PM
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarat M Kumar wrote:getAppDate() and setCreateDate() uses Date Objects
Batch job logic : select * from USER where CREATE_DATE BETWEEN (sysdate-10/1440) AND sysdate;
sysdate-10/1440 : returns 10 minutes before current sysdate of DB .

DB server is running at "Newyork" with EST timezone


Yes, but, as many have tried to explain, that has nothing to do with the date (or time, if you prefer) that's stored.

You actually have a second issue, which is that you're dealing with two separate things: the database, and Java - and they haven't always seen eye to eye. However, if you stick to proper JDBC practice as advised above, you shouldn't have any problems.

Remember: dates (and in particular, java.util.Date's) have no knowledge of timezones. It's only when you need to display them that TZ comes into play.

You may find this page useful, although it's still a bit of a "work in progress".

Winston
 
Sarat M Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaDoc for java.util.Calendar has answered my confusion . Thanks a lot for all the people who helped me .I am drafting my findings,this might be helpful to anyone in the world:
As perJava Doc for below methods:
A) Calendar.getInstance() : - method returns a Calendar object whose only calendar fields have been initialized with the current date and time.

B)Calendar rightNow=Calendar.getInstance(TimeZone)
-method returns a Calendar object whose only calendar fields have been initialized with the current date and time in the given TimeZone.
Refer the below code & op to explain the above lines:

Output for above code :
Hour in Newyork 15
Day in Newyork 9
Hour in India 2
Day in India 10
Date in Newyork timezone Thu Jan 10 02:23:57 IST 2013
Date in India timezone Thu Jan 10 02:23:57 IST 2013
As per the Java DOC :
The calendar field values can be set by calling the set methods. Any field values set in a Calendar will not be interpreted until it needs to calculate its time value (milliseconds from the Epoch) or values of the calendar fields. Calling the get, getTimeInMillis, getTime, add and roll involves such calculation.
Let me explain the above lines with a example:

Output for the above code :
Hour in Newyork 15
Day in Newyork 9
Hour in India 2
Day in India 10
Date in Newyork timezone Thu Jan 10 02:27:53 IST 2013
Date in India timezone Thu Jan 10 02:27:53 IST 2013
Time recomputed , so the date in New york Wed Jan 09 15:27:53 IST 2013 [ hour & day fields were set to newyork timezone values, getTime() method invoked the re calculation of time in mills secs , so we are seeing newyork values in date object ]

Conclusion: Calendar.getInstance(TimeZone) method sets "isTimeSet" flag to false .Hence Recomputation of TimeInMillsSec is not happening .
Thanks a lot for all , who help me ..
Not sure if I explained my understanding properly . Now I am clear with concepts of calendar , timezone, date..




 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarat M Kumar wrote:Conclusion: Calendar.getInstance(TimeZone) method sets "isTimeSet" flag to false .Hence Recomputation of TimeInMillsSec is not happening .
Thanks a lot for all , who help me ..
Not sure if I explained my understanding properly . Now I am clear with concepts of calendar , timezone, date.


I really hope you are, because that seems like an awful lot of code to me.

The fact of the matter is that you absolutely should NOT be setting (ie, changing) any times to conform to New York time; and if you are, whoever suggested that you do should be hung over a slow fire by his tender parts.

Java (and database) time values are the same the world over; and the ONLY time you should ever involve timezones is for display.

Please read the page I suggested if you still aren't convinced.

Winston

BTW: Please DontWriteLongLines (←click) inside code blocks. I've broken yours up this time.
reply
    Bookmark Topic Watch Topic
  • New Topic