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

 
 
Arne Vajhj
Guest
Posts: n/a
 
      07-01-2009
Robert Dodier wrote:
> 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).


That is relative straightforward.

Example:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ManualParse {
private static Pattern re =
Pattern.compile("(?:\\d{4}-\\d{2}-\\d{2}
\\d{2}:\\d{2}:\\d{2})([+-]\\d{4})");
public static int parseOffset(String s) {
Matcher m = re.matcher(s);
if(m.find()) {
String offset = m.group(1);
return Integer.parseInt(offset.substring(0, 3)) * 60 +
Integer.parseInt(offset.substring(3));
} else {
throw new IllegalArgumentException("No offset in string");
}
}
public static void main(String[] args) {
System.out.println(parseOffset("2009-06-26 14:13:00-0400"));
}
}

Arne

 
Reply With Quote
 
 
 
 
Mayeul
Guest
Posts: n/a
 
      07-01-2009
Arne Vajhøj wrote:
> 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.


Sure, but parsing a Date and the TimeZone it is indicated to use, is the
purpose of which class, then?

Admittedly, it is fairly straightforward to just program it. But,
DateFormat.parse() does parse the time zone or offset information, to
produce the Date object. And then it discards it without any mean to
fetch this information, leaving no choice but to program something to
parse this information again. Not a big deal, but it surprises me.

--
Mayeul
 
Reply With Quote
 
 
 
 
Mayeul
Guest
Posts: n/a
 
      07-01-2009
Mayeul wrote:
> Arne Vajhøj wrote:
>> 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.

>
> Sure, but parsing a Date and the TimeZone it is indicated to use, is the
> purpose of which class, then?
>
> [snip]


Disregard that. I hadn't read the code snippet that shows
DateFormat.parse() will change the TimeZone associated with the DateFormat.

--
Mayeul
 
Reply With Quote
 
Robert Dodier
Guest
Posts: n/a
 
      07-01-2009
On Jun 30, 7:28*pm, "John B. Matthews" <(E-Mail Removed)> wrote:

> Something like that?


Actually I am working with physical data for which
local time of day is a factor.

best

Robert Dodier
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      07-01-2009
In comp.lang.java.programmer message <9_SdnRn_LruYz9fXnZ2dnUVZ8sidnZ2d@b
t.com>, Tue, 30 Jun 2009 18:57:25, RedGrittyBrick
<(E-Mail Removed)> posted:
>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?

>
>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.



They do not have the same offset, it being Northern Summer. And, of
those who do vary their clocks, London does not use "DST" but uses
"Summer Time", and I expect Lisbon does similarly in Portuguese and
Tenerife in Spanish.

Morocco, when last I checked, had unique Time Rules; Iceland is
permanently GMT; Tenerife is Spanish and its clocks always agree with
Lisbon and London.

One can only get a Time Zone from an Offset if the time is known to be
in Winter for that location. If the time is known to be in Summer
there, one also needs to know whether or not the location is LHI.

If one can get a time in January and a time in July, with different
offsets, one can deduce the Hemisphere (N/S) and the Zone - except, in
principle, for any who do not set their clocks forward from Standard
Time in Summer but instead set them back from Standard Time in Winter
(which, after all, is normally shorter). It was thought that the Irish
did that 1972; but they are not nowadays allowed to do so.

I have demonstration code for that, in javaSCRIPT, on my site.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      07-05-2009
Mayeul wrote:
> Arne Vajhøj wrote:
>> 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.

>
> Sure, but parsing a Date and the TimeZone it is indicated to use, is the
> purpose of which class, then?
>
> Admittedly, it is fairly straightforward to just program it. But,
> DateFormat.parse() does parse the time zone or offset information, to
> produce the Date object. And then it discards it without any mean to
> fetch this information, leaving no choice but to program something to
> parse this information again. Not a big deal, but it surprises me.


DateFormat is a class that converts between textual representations
of time and Date which is a true time.

There are no place for returning offset in that.

If there were a TimeOffset and a TimeOffsetFormat class, then ...

But it is not in standard Java. And I don't think it is in JODA
or ICU4J either, so apparently the demand has not been big enough.

Arne
 
Reply With Quote
 
Janin Janin is offline
Junior Member
Join Date: Aug 2013
Location: California
Posts: 1
 
      08-08-2013
I was searching for the answer to this myself, and as it turns out, there is no built-in method to do this. You can do it yourself, but it's difficult because, as others have mentioned, location (country, state, even city), and daylight savings time are a factor. The easiest way to get the actual time zone is to have it stored in the first place.

The only time time zone is needed is to display a time in human-readable form. Otherwise, the UTC time in milliseconds is sufficient to pinpoint a time and use it for comparison.
 
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