aspose file tools*
The moose likes Java in General and the fly likes Date problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date problem" Watch "Date problem" New topic
Author

Date problem

Sarat M Kumar
Greenhorn

Joined: Jan 09, 2013
Posts: 8
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?
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1059
    
  10

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?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Jan 09, 2013
Posts: 8
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
[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

Joined: Jan 09, 2013
Posts: 8
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Forget the Calendar and TimeZone, they are irrelevant.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Jan 09, 2013
Posts: 8
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Jan 09, 2013
Posts: 8
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

Joined: Jan 09, 2013
Posts: 8
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
[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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Jan 09, 2013
Posts: 8
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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7816
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Sarat M Kumar
Greenhorn

Joined: Jan 09, 2013
Posts: 8
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

Joined: Mar 17, 2011
Posts: 7816
    
  21

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date problem