capturing timezone when parsing java.util.Date

Discussion in 'Java' started by Robert Dodier, Jun 26, 2009.

  1. 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
    Robert Dodier, Jun 26, 2009
    #1
    1. Advertising

  2. Robert Dodier

    Roedy Green Guest

    On Fri, 26 Jun 2009 14:06:19 -0700 (PDT), Robert Dodier
    <> wrote, quoted or indirectly quoted someone
    who said :

    >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
    Roedy Green, Jun 27, 2009
    #2
    1. Advertising

  3. On Jun 26, 6:05 pm, Roedy Green wrote:

    > see http://mindprod.com/jgloss/calendar.html


    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
    Robert Dodier, Jun 29, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    98,540
    vongeuca
    May 4, 2011
  2. asaf
    Replies:
    3
    Views:
    6,136
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Sep 11, 2006
  3. Replies:
    5
    Views:
    5,583
  4. Robert Dodier
    Replies:
    36
    Views:
    4,122
    Janin
    Aug 8, 2013
  5. grz01
    Replies:
    3
    Views:
    2,317
    Arne Vajhøj
    Sep 18, 2009
Loading...

Share This Page