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

 
 
Robert Dodier
Guest
Posts: n/a
 
      06-26-2009
Hello,

When a string like "2009-06-26 14:13:00-0400" is parsed to
a java.util.Date via java.text.SimpleDateFormat, the timezone
in the string is lost --- the timezone of the result isn't UTC-04:00,
instead it's the default timezone (or date formatter's timezone,
if it was assigned a non-default value).

I could pull off the trailing timezone from the string and parse
it separately and adjust the timezone of the date by hand,
but I can't see a way to do that. java.util.Date doesn't have a
method to change the timezone. java.util.Calendar has
setTimeZone, but the following:

java.util.Date d0 = <whatever>;
java.util.Calendar c = java.util.Calendar.getInstance ();
c.setTime (d0);
c.setTimeZone (<whatever>);
d1 = c.getTime ();

yields a date which has the default timezone.
I can't see another way to do it with Calendar.

Any advice about how to capture the timezone when parsing
a date would be appreciated. Also, if someone wants to
recommend a different time/date library, I would be interested.
Java's built-in time/date functions are a colossal disaster,
but I digress.

best

Robert Dodier


 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      06-27-2009
Robert Dodier wrote:
> Hello,


Once was enough to post your query.

> When a string like "2009-06-26 14:13:00-0400" is parsed to
> a java.util.Date via java.text.SimpleDateFormat, the timezone
> in the string is lost --- the timezone of the result isn't UTC-04:00,
> instead it's the default timezone (or date formatter's timezone,
> if it was assigned a non-default value).
>
> I could pull off the trailing timezone from the string and parse
> it separately and adjust the timezone of the date by hand,
> but I can't see a way to do that. java.util.Date doesn't have a
> method to change the timezone. java.util.Calendar has
> setTimeZone, but the following:
>
> java.util.Date d0 = <whatever>;


Don't forget the 'import' feature.

> java.util.Calendar c = java.util.Calendar.getInstance ();
> c.setTime (d0);
> c.setTimeZone (<whatever>);
> d1 = c.getTime ();
>
> yields a date which has the default timezone.
> I can't see another way to do it with Calendar.
>
> Any advice about how to capture the timezone when parsing
> a date would be appreciated. Also, if someone wants to
> recommend a different time/date library, I would be interested.
> Java's built-in time/date functions are a colossal disaster,
> but I digress.


Date objects don't contain timezone information.

From <http://java.sun.com/javase/6/docs/api/java/util/Date.html>:
> the Date class is intended to reflect coordinated universal time (UTC)


Use a combination of java.util.Calendar and java.text.DateFormat.

--
Lew
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      06-27-2009
In article <4a453bfb$(E-Mail Removed)>,
Robert Dodier <(E-Mail Removed)> wrote:

> When a string like "2009-06-26 14:13:00-0400" is parsed to a
> java.util.Date via java.text.SimpleDateFormat, the timezone in the
> string is lost --- the timezone of the result isn't UTC-04:00,
> instead it's the default timezone (or date formatter's timezone, if
> it was assigned a non-default value).


When the string "2009-06-26 14:13:00-0400" is parsed, the resulting Date
doesn't have a Timezone; it is merely the number of milliseconds since
the epoch, UT. It can be formatted to show the same Date seen on a clock
in any Timezone:

<console>
2009-06-26 14:13:00GMT-04:00 GMT-04:00 1246039980000
2009-06-26 14:13:00EDT US/Eastern 1246039980000
2009-06-26 19:13:00BST Europe/London 1246039980000
2009-06-26 20:13:00CEST Europe/Berlin 1246039980000
</console>

<code>
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TestSDF {

private static final String s = "yyyy-MM-dd HH:mm:sszz";
private static final DateFormat f = new SimpleDateFormat(s);

public static void main(String[] args) {
try {
String str = "2009-06-26 14:13:00-0400";
Date date = f.parse(str);
print("GMT-04:00", date);
print("US/Eastern", date);
print("Europe/London", date);
print("Europe/Berlin", date);
} catch (ParseException e) {
e.printStackTrace(System.err);
}
}
private static void print(String tz, Date d) {
f.setTimeZone(TimeZone.getTimeZone(tz));
System.out.println(f.format(d)
+ " " + tz
+ " " + d.getTime());
}
}
</code>

[...]
> Any advice about how to capture the timezone when parsing
> a date would be appreciated.


"You can use the getAvailableIDs method to iterate through all the
supported time zone IDs," or use a CustomID, described here:

<http://java.sun.com/javase/6/docs/api/java/util/TimeZone.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      06-27-2009
In comp.lang.java.programmer message <4a453bfb$(E-Mail Removed)>,
Fri, 26 Jun 2009 17:22:03, Robert Dodier <(E-Mail Removed)>
posted:

>When a string like "2009-06-26 14:13:00-0400" is parsed to
>a java.util.Date via java.text.SimpleDateFormat, the timezone
>in the string is lost --- the timezone of the result isn't UTC-04:00,


That which is in the string is not a Time Zone, it is a Time Offset.

Time Zones govern, or are governed by, Winter Time.

--
(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
 
Roedy Green
Guest
Posts: n/a
 
      06-28-2009
On Sat, 27 Jun 2009 22:58:53 +0100, Dr J R Stockton
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>Time Zones govern, or are governed by, Winter Time.


except in the Southern hemisphere.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Deer hunting would be fine sport, if only the deer had guns."
~ William S. Gilbert of Gilbert and Sullivan
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      06-28-2009
In comp.lang.java.programmer message <frid455v3ard1na2933517ml5th8mer2bp
@4ax.com>, Sat, 27 Jun 2009 18:46:37, Roedy Green <see_website@mindprod.
com.invalid> posted:
>On Sat, 27 Jun 2009 22:58:53 +0100, Dr J R Stockton
><(E-Mail Removed)> wrote, quoted or indirectly quoted
>someone who said :
>
>>Time Zones govern, or are governed by, Winter Time.

>
>except in the Southern hemisphere.



No. Remember that they have winter in the middle of the Gregorian
calendar year.

Chile and Labrador are both in the zone four hours behind Greenwich,
But currently Labrador clocks are two hours ahead of Chilean ones, and
at Christmas Chilean ones will be two hours ahead of Labrador ones.

Few Time Zones contain populous parts that advance the clocks in the
first half of the calendar year and other populous parts that advance
the clocks in the second half of the calendar year, AFAICS; although
there's a fair area north of AU/NZ that advances its clocks in Summer.

--
(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
 
Robert Dodier
Guest
Posts: n/a
 
      06-29-2009
On Jun 26, 6:08*pm, Lew <(E-Mail Removed)> wrote:

> Robert Dodier wrote:
> > Hello,

>
> Once was enough to post your query.


Gosh, Lew, I'll be more careful next time.

> > *java.util.Date d0 = <whatever>;

>
> Don't forget the 'import' feature.


Aha! So *that* is why is doesn't work.
Wait a minute, "<whatever>" isn't valid syntax.
I think something has escaped your eagle eye!

Keep up the good work!

Robert Dodier
 
Reply With Quote
 
Robert Dodier
Guest
Posts: n/a
 
      06-29-2009
On Jun 27, 3:58*pm, Dr J R Stockton <(E-Mail Removed)>
wrote:

> That which is in the string is not a Time Zone, it is a Time Offset.


OK. Is there a way to capture it? I could do it by hand,
but I'd like to get Java to do it for me.

Thanks for your help,

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

> When the string "2009-06-26 14:13:00-0400" is parsed,
> the resulting Date doesn't have a Timezone; it is merely
> the number of milliseconds since the epoch, UT.


Understood. Is there a Java function which records both
the milliseconds since the epoch and the timezone or offset?

> "You can use the getAvailableIDs method to iterate through all the
> supported time zone IDs," or use a CustomID, described here:


Yes, but that doesn't extract the timezone ID from an input string.

Thanks for your help,

Robert Dodier
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-29-2009
Robert Dodier wrote:
> Understood. Is there a Java function which records both
> the milliseconds since the epoch and the timezone or offset?


As mentioned upthread, java.util.Calendar has that.

> Yes, but that doesn't extract the timezone ID from an input string.


As mentioned upthread, java.text.DateFormat, in particular
SimpleDateFormat, has that.

--
Lew

 
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