This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
Joined: Jul 26, 2011
0. Read what you wrote.
1. From ui: 01/31/2013
2. To server: Thu Jan 31 00:00:00 PST 2013
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?
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.
Joined: Mar 05, 2008
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.
Joined: Jul 26, 2011
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:
subject: How to get timezone from a given java.util.Date.