aspose file tools*
The moose likes Java in General and the fly likes Changing the timezone of a date, not only display Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Changing the timezone of a date, not only display" Watch "Changing the timezone of a date, not only display" New topic
Author

Changing the timezone of a date, not only display

Rohits Shah
Greenhorn

Joined: Apr 03, 2008
Posts: 2
Hi,

I am getting the date from the database as type java.util.Date, therefore it takes the timezone of my machine
i.e. EST, now if I want to change its timezone to GMT, not of the display but the date itself, does anybody have a solution for this?

Thanks in advance,
rshah82
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

In java.util.Calendar you can set time zones.
So, you can basically create a Calendar object with the same time zone as Date. Use setTime(Date date) method to set the date object that you have and then change the time zone using setTimeZone().


apigee, a better way to API!
Rohits Shah
Greenhorn

Joined: Apr 03, 2008
Posts: 2
Actually the solution does not seem to work the way I need it, what I actually need is as follows:

1. I have an DB where I store information in timestamp format without timezone.
2. I get that timetsamp from db in the form of java.util.Date from hibernate and as discussed java.util.Date does not store the timezone
3. But the java.util.Date appends the timezone of the application server to the information its carries, so it assumes that whatever timezone it gets from the 'Application Server' timezone and appends it to the info. You can see that by printing a java.util.Date variable, it displays the timezone.
4. So suppose if now I am getting Apr 4 2008 7:00 from the DB into a java.util.Date variable, the java.util.Date by itself will append EST as my application server is in EST but I want to append GMT to it somehow. Like I want Apr 4 2008 7:00 GMT and not Apr 4 2008 7:00 EST.
5. This is just beacuse I would then convert into whatever timezone I need and display, like for the above I would display Apr 4 2008 3:00 EST

I guess this small example should be more useful.

Thanks again,
Rohits
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

rohits: I have an DB where I store information in timestamp format without timezone.

Oh I dont think this is correct. I assume you always store and read it using the same timezone. People generally store it in GMT.

rohits: So suppose if now I am getting Apr 4 2008 7:00 from the DB into a java.util.Date variable, the java.util.Date by itself will append EST as my application server is in EST but I want to append GMT to it somehow. Like I want Apr 4 2008 7:00 GMT and not Apr 4 2008 7:00 EST.

I think your problem is that hibernate thinks that the timezone is EST whereas you have stored the time as GMT.
A casual search gave me this article. May be it wil help.
I have no idea about hibernate. May be you will get better results asking this question on the hibernate forum
Kiran Dalvi
Greenhorn

Joined: Dec 07, 2004
Posts: 7
I hope you solved your problem. I had exact same requirement and here is how I got it -

This does convert the time/date to UTC as well as displays as "UTC" when printed as String.

public class UTCTime {

public static Date getCurrentUTCTime() {
Calendar cal = Calendar.getInstance(Locale.getDefault());
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

return cal.getTime();
}
}
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4540
    
    5

java.util.Date does not have a timezone. The class represents an instant in time, internally held as a long which is the number of milliseconds since the Epoch.


luck, db
There are no new questions, but there may be new answers.
Kiran Dalvi
Greenhorn

Joined: Dec 07, 2004
Posts: 7
You are absolutely correct. java.util.Date does not have a Time Zone associated with it.
The issue here was when printing toString() on a Date object, it does show a TimeZone (Of an AppServer/Local Machine) and we needed to change that displayed value as well.

public class UTCTimeTester {
public static void main(String[] args) {
System.out.println("Current UTC Time = " + UTCTime.getCurrentUTCTime());
}
}

Output -
Current UTC Time = Mon May 21 08:06:55 UTC 2012
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4540
    
    5

Kiran Dalvi wrote:You are absolutely correct. java.util.Date does not have a Time Zone associated with it.
The issue here was when printing toString() on a Date object, it does show a TimeZone (Of an AppServer/Local Machine) and we needed to change that displayed value as well.


That's what a DateFormat is for.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

The only reason why you now see the date printed in UTC is because you are setting the default timezone of the JVM to UTC with this line:

Note that now all Date objects will be printed with UTC, not just the Date object that is returned by the method.

That's most likely not what you want!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kiran Dalvi
Greenhorn

Joined: Dec 07, 2004
Posts: 7
Ok.
So, how do we solve this problem? I want to get the current date (java.util.Date) from this class and should be UTC. DateFormat gives me only display. I want the Date object to contain UTC time.
I appreciate your help.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kiran Dalvi wrote: I want the Date object to contain UTC time.


Impossible, since Date objects do not have a TZ. I'm not sure why you're asking, since you already stated that you understand this fact.

A Date object just represents an instant in time. "Now" is "now", no matter what TZ you are in (if we ignore special relativity, which I think it's safe to do here ).
Kiran Dalvi
Greenhorn

Joined: Dec 07, 2004
Posts: 7
Thanks. I decided to use Calendar for display purposes and Date only for manipulation/DB updates. Here is what I came up with -


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

You're making it much more complicated than necessary.

Why don't you just use a DateFormat to display the date in whatever timezone you want? Here's how you could write your displayUTCTime() method:

What your getDate() method does is a little bit strange. A Date object represents an absolute moment in time, independent of any time zone. Your getDate() method just takes the current time ("now") and subtracts 5 hours and 30 minutes from it. What you're getting is not the time in UTC, but a Date object that represents "now" minus 5 hours and 30 minutes, regardless of the time zone.

Your decrementHours() and decrementMinutes() methods have confusing names, because what they do is add an offset (which happens to be negative).
Kiran Dalvi
Greenhorn

Joined: Dec 07, 2004
Posts: 7
Thanks for the reply. I am trying to fulfill 2 requirements -
1. Whenever I display date+time on my site, it should be UTC.
2. All calculations on the Date+Time should happen on UTC alongwith DB persistance.
3. My AppServer runs in India (UTC+5.5)

With your solution, I can achieve (1) but can't possibly achieve(2).

With -
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
I can achieve it. Only issue would be in case I need to use my local time somewhere in future, I would be handicapped.
So I am trying to go around it.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4540
    
    5

Kiran Dalvi wrote:Thanks for the reply. I am trying to fulfill 2 requirements -
1. Whenever I display date+time on my site, it should be UTC.
2. All calculations on the Date+Time should happen on UTC alongwith DB persistance.
3. My AppServer runs in India (UTC+5.5)

With your solution, I can achieve (1) but can't possibly achieve(2).

The long value internally stored by a Date instance doesn't get changed by setting the default TimeZone. It doesn't depend on the TimeZone at all, only on the number od milliseconds since the Epoch.

The Epoch was the same instant of time everywhere.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Darryl Burke wrote:
Kiran Dalvi wrote:Thanks for the reply. I am trying to fulfill 2 requirements -
1. Whenever I display date+time on my site, it should be UTC.
2. All calculations on the Date+Time should happen on UTC alongwith DB persistance.
3. My AppServer runs in India (UTC+5.5)

With your solution, I can achieve (1) but can't possibly achieve(2).

The long value internally stored by a Date instance doesn't get changed by setting the default TimeZone. It doesn't depend on the TimeZone at all, only on the number od milliseconds since the Epoch.

The Epoch was the same instant of time everywhere.


So, IF your database handles times correctly, and IF you have the correct column definition, then all you need to do is call PreparedStatement.setTimestamp(int parameterIndex, Timestamp x) to store the date and time, and the corresponding ResultSet.getTimestamp() to retrieve it. It won't matter what TZ your server or your client is in--you'll have the instant in time, independent of any TZ, and you can format it for whatever TZ you want when it comes time to display it.

Now, depending on how your database handles times, you may instead need setTimestamp(int parameterIndex, Timestamp x, Calendar cal) and the corresponding getTimestamp(String columnLabel, Calendar cal). If I recall correctly, I've seen some inconsistencies and non-intuitive behaviors across different DBs' handling of times, but one of these method pairs should be all you need. You'll just have to do some testing with different combinations to make sure you get what you expect.
Kiran Dalvi
Greenhorn

Joined: Dec 07, 2004
Posts: 7
Thanks. Much more clear now.
Looks like changing JVM TimeZone suits the best to what I am looking for.

Below does give me the Date which I am looking for.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kiran Dalvi wrote:Thanks. Much more clear now.
Looks like changing JVM TimeZone suits the best to what I am looking for.


That doesn't sound right, and it's certainly not the message that anybody here was trying to convey.
Kiran Dalvi
Greenhorn

Joined: Dec 07, 2004
Posts: 7
Thanks Jeff.
I will try the handle insert/update on DB side using Timestamp as you suggested [as opposed to playing with the original Date object]. For displaying, I will use DateFormat.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Jeff Verdegan wrote:
Now, depending on how your database handles times, you may instead need setTimestamp(int parameterIndex, Timestamp x, Calendar cal) and the corresponding getTimestamp(String columnLabel, Calendar cal). If I recall correctly, I've seen some inconsistencies and non-intuitive behaviors across different DBs' handling of times, but one of these method pairs should be all you need. You'll just have to do some testing with different combinations to make sure you get what you expect.

I'm using this method to set data types which do not hold timezone information; the passed Timestamp time instant is stored in (ie. converted to) the timezone of given Calendar instance.

Any problems probably have to do not with databases, but with JDBC drivers. I've discovered this bug in jTDS driver recently. (I've dug quite deep on this issue and some notes in the source code seem to indicate that the developers of the driver were not exactly sure what this method is supposed to do; though the bug does not stem from such misunderstanding - the basics are right, just the Daylight Saving Time transition is not handled properly.)

I sincerely advise to concentrate the testing on Daylight Saving Time transitions of all participating time zones...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing the timezone of a date, not only display