Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > capturing timezone when parsing java.util.Date

Reply
Thread Tools

capturing timezone when parsing java.util.Date

 
 
Lew
Guest
Posts: n/a
 
      06-30-2009
Patricia Shanahan wrote:
>> Presumably, I need to store both a Date and a TimeZone, but how do I get
>> the TimeZone from parsing the original String?

>


RedGrittyBrick wrote:
> You don't?
>
> By TimeZone you obviously mean java.util.TimeZone which, according to
> the API, "represents a time zone offset, and also figures out daylight
> savings."
>
> In the general case I suspect you can't get a DST-aware TimeZone from an
> offset alone. Consider that Dakar, Casablanca, London, Lisbon, Reykjavk
> and Tenerife have the same offset but probably have different policies
> for DST.
>


Heck, until recently the southern part of Indiana in the U.S. had
different policies for DST than the rest of the state. (They didn't
have it at all.)

--
Lew
 
Reply With Quote
 
 
 
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      06-30-2009
Patricia Shanahan wrote:

> Presumably, I need to store both a Date and a TimeZone, but how do I get
> the TimeZone from parsing the original String?


As already pointed out by various people you can't get the
timezone from a date containing only the offset. Without
trying it out, at least getting the offset-information might
work like this:

Parse the date using "Z" as pattern and timezone "UTC" using
SimpleDateFormatter. That should return Jan 01 1970 plus or
minus the time the offset was defining, so a date
....+01:00
should return the date
Dec 31 1969 23:00:00 or Jan 01 1970 01:00:00
(I'm too lazy at the moment to think what is correct

To get the offset it might already be enough to call
getTime() on that date-instance to get the offset in
milliseconds.

Again: I haven't tried that so I might be wrong
Again2: That only gives you the time-offset of that particular
date being parsed. You still have no clue what timezone the
creator of the date was in.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: http://www.velocityreviews.com/forums/(E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
 
 
 
Robert Dodier
Guest
Posts: n/a
 
      06-30-2009
On Jun 30, 1:35*pm, Michal Kleczek <(E-Mail Removed)> wrote:

> public static void main(String[] args) throws ParseException {
>
> * final DateFormat dateFormat =
> * * new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
> * dateFormat.parse("2009-06-26 14:13:00 CET");
> * System.out.println(dateFormat.getTimeZone().getDis playName());
> * dateFormat.parse("2009-06-26 14:13:00 EST");
> * System.out.println(dateFormat.getTimeZone().getDis playName());
>
> }
>
> Prints:
> Central European Time
> Eastern Standard Time


Thanks for this. When I try this, it appears that I get my
local time zone if the time zone offset is specified as a
numeric value (e.g. -0400) instead of a alphabetic abbreviation
(e.g. EDT). The values I have to work with have numeric
offsets. I also tried Z instead of z in the format; same behavior.

Thanks for your help,

Robert Dodier
 
Reply With Quote
 
Robert Dodier
Guest
Posts: n/a
 
      06-30-2009
On Jun 29, 5:52*pm, "John B. Matthews" <(E-Mail Removed)> wrote:

> I'm curious what you'll do with the information.


I need to recover the local hour of the day.

FWIW

Robert Dodier
 
Reply With Quote
 
Robert Dodier
Guest
Posts: n/a
 
      06-30-2009
On Jun 30, 11:57*am, RedGrittyBrick <(E-Mail Removed)>
wrote:

> In the general case I suspect you can't get a DST-aware
> TimeZone from an offset alone. Consider that Dakar,
> Casablanca, London, Lisbon, Reykjavk and Tenerife
> have the same offset but probably have different
> policies for DST.


OP here. I would be happy enough if I could get some
Java method to parse a numeric offset (along with the
rest of the date).

I don't actually need the timezone, just the offset.
Thanks to those who pointed out the difference.

Robert Dodier
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-30-2009
Robert Dodier wrote:
> Thanks for this. When I try this, it appears that I get my
> local time zone if the time zone offset is specified as a
> numeric value (e.g. -0400) instead of a alphabetic abbreviation
> (e.g. EDT). The values I have to work with have numeric
> offsets. I also tried Z instead of z in the format; same behavior.


Does the trick of creating a 'TimeZone' object from the "GHT-04:00"
part of the string work for you? (If all you have is "-04:00" you can
concatenate it to the "GMT".)

--
Lew
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      07-01-2009
Mayeul wrote:
> Lew wrote:
>> Robert Dodier wrote:
>>> Yes. I have concluded that I'll have to parse the timezone
>>> offset by hand and store it separately.

>>
>> Or you could use java.text.DateFormat to parse the timezone information
>> <http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html#parse(java.lang.String)>
>>
>> using the parsing pattern "Z" at the end of the pattern string in a
>> java.text.SimpleDateFormat
>> <http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html>

>
> Maybe it's just me, but I still can't figure out how can
> DateFormat.parse() give any sort of information about the time zone or
> offset of the date it just parsed.


Id o not return it - it just uses it.

> Which kinda surprises me, actually.


Not really the purpose of the class, so it does not surprise me.

Arne
 
Reply With Quote
 
Arne Vajhj
Guest
Posts: n/a
 
      07-01-2009
Patricia Shanahan wrote:
Even after reading the Javadocs, as well as this discussion, I still
> don't know how to deal with the following use-case: Record a date in a
> format that allows for time comparisons and adjustments, but display it
> appropriately for the original time zone.
>
> It's easy if I want to display all dates in the current locale, or in
> some specified locale. I just parse to get a Date, do my comparisons
> etc. on that Date object, and then format to the required locale.
>
> It is not so easy, as far as I can tell, if I want to display each date
> in its original time zone.
>
> Presumably, I need to store both a Date and a TimeZone,


Obviously.

> but how do I get
> the TimeZone from parsing the original String?


I would just parse it manually.

Special functionality requires special code.

Arne
 
Reply With Quote
 
Arne Vajhj
Guest
Posts: n/a
 
      07-01-2009
Michal Kleczek wrote:
> Patricia Shanahan wrote:
>> Even after reading the Javadocs, as well as this discussion, I still
>> don't know how to deal with the following use-case: Record a date in a
>> format that allows for time comparisons and adjustments, but display it
>> appropriately for the original time zone.

>
> Assuming there is original timezone encoded in date/time string:
>
> public static void main(String[] args) throws ParseException {
>
> final DateFormat dateFormat =
> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
> dateFormat.parse("2009-06-26 14:13:00 CET");
> System.out.println(dateFormat.getTimeZone().getDis playName());
> dateFormat.parse("2009-06-26 14:13:00 EST");
> System.out.println(dateFormat.getTimeZone().getDis playName());
>
> }
>
> Prints:
> Central European Time
> Eastern Standard Time


Very interesting.

I don't like it. It is very bad in my opinion that the parse method
as a side effect changes the Calendar/TimeZone of the DateFormat.

It is not very obvious from the docs either. I wonder whether it is
really a Java feature or an implementation feature.

Arne
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      07-01-2009
In article
<(E-Mail Removed)>,
Robert Dodier <(E-Mail Removed)> wrote:

> On Jun 29, 5:52*pm, "John B. Matthews" <(E-Mail Removed)> wrote:
>
> > I'm curious what you'll do with the information.

>
> I need to recover the local hour of the day.
>
> FWIW


I was trying to imagine a use case, as others have asked above: You're
in the Paris office checking the time stamp on a transaction conducted
at the New York branch. The date & time might be rendered using
TimeZone.getTimeZone("GMT") and Locale.FRANCE. You need to know what the
clock in New York said at that time, perhaps to check a local deadline.
You format the Date with a DateFormat having TimeZone.getTimeZone(
"America/New_York"). Alternatively, you construct a Date based on the
deadline in the New York time zone and compare it to the time stamp.

Something like that?

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
capturing timezone when parsing java.util.Date Robert Dodier Java 2 06-29-2009 04:55 PM
TimeZone change - how to use TimeZone class axrock Java 8 03-25-2009 11:54 PM
TimeZone calculation on Windows Vista with DateTime::TimeZone Ami Perl Misc 5 09-24-2007 07:27 AM
Convert windows TimeZone to Java TimeZone asaf Java 3 09-11-2006 05:40 PM
parsing time string, getting timezone Ilja Booij C Programming 3 09-10-2004 12:24 PM



Advertisments