capturing timezone when parsing java.util.Date

R

Robert Dodier

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
 
R

Roedy Green

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

Date's don't support timezones anymore. You see a GregorianCalendar
object instead.

see http://mindprod.com/jgloss/calendar.html
--
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
 
R

Robert Dodier


Thanks, I looked at the parsing example. From what I can tell,
the calendar timezone is only whatever was specified
a priori; it cannot be captured from the input date string.
I could try to handle it myself --- parse the string once
to get the timezone and then again to get the time & date.
I'm trying to get Java to do that work for me.
Can you see a way to do it? Thanks for your help.

Robert Dodier
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top