aspose file tools*
The moose likes Beginning Java and the fly likes How to get timezone from a given java.util.Date. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get timezone from a given java.util.Date." Watch "How to get timezone from a given java.util.Date." New topic
Author

How to get timezone from a given java.util.Date.

H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
Hi & Thanks,

Below is how I get timezone from a given java.util.Date. Is it correct?





Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
A java.util.Date does not have a time zone associated with it. Its toString() method may make it appear to have a time zone, but it doesn't. The time zone provided by toString() is the system default time zone.

A Calendar does have a time zone associated with it. And that appears to be what you are trying to do. So for the rest of this post, I will ignore the question about java.util.Date, since you don't seem to be using those here.

The "timezone" needed by XMLGregorianCalendar is an int, which is described in documentation as an offset in minutes. Presumably, this means how many minutes is the time zone off from GMT. Presumably you need to include any daylight savings effects here, since there's no separate way to specify this.

If you want to get the TimeZone from another Calendar and use that to specify the timezone for the XMLGregorianCalendar, you need to find the offset for that time zone. You're trying to use the SHORT static constant, which is an int, but other than that, has nothing at all to do with what you need here. I recommend using either the getOffset() or getRawOffset() methods here. The raw offset will not include daylight savings; if you need that, use getOffset. Note that these methods return an offset in milliseconds, and you need an offset in minutes. You will need to do some basic math to convert these.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
0. Read what you wrote.

1. From ui: 01/31/2013
2. To server: Thu Jan 31 00:00:00 PST 2013

3.


4. If I used timezone getRawOffset which happened to -28800000, I got below exception:




5. If I used timezone SHORT which has 0 and it's OK. Is this by chance due to PST?

from doc:

public static final int SHORT
A style specifier for getDisplayName() indicating a short name, such as "PST."

6. There is something I did not get. Thanks.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
H Paul wrote:4. If I used timezone getRawOffset which happened to -28800000, I got below exception:

Mike Simmons wrote:Note that these methods return an offset in milliseconds, and you need an offset in minutes. You will need to do some basic math to convert these.

In other words, how many minutes are there in 28800000 milliseconds?

H Paul wrote:5. If I used timezone SHORT which has 0 and it's OK.

Well, it may compile and run without an obvious error, but is the result really accurate? I doubt it.

H Paul wrote:Is this by chance due to PST?

Not really. They mentioned PST in the JavaDoc as an example, saying that "PST" is the short name for "Pacific Standard Time". TimeZone.SHORT is a constant for "use the short form of the name", and TimeZone.LONG is a constant for "use the long form of the name". But you're using this constant in a completely different context where it makes no sense. It's probably better to just forget about this SHORT thing; it's just a distraction from what you need to understand here.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
1. Thanks Mike, I need to do some math as suggested. Back to school.

2. In meantime, as I look back what I needed was newXMLGregorianCalendar:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get timezone from a given java.util.Date.