Help with date conversion

Discussion in 'Java' started by Jack, Sep 24, 2004.

  1. Jack

    Jack Guest

    How to convert:

    yyyy-MM-ddThh:mm:ss.sTZD

    to

    yyyy-MM-dd HH:mm:ss

    ?

    thanks for any tips
     
    Jack, Sep 24, 2004
    #1
    1. Advertising

  2. Jack

    Bryce Guest

    On Fri, 24 Sep 2004 15:35:25 GMT, "Jack"
    <> wrote:

    >
    >How to convert:
    >
    >yyyy-MM-ddThh:mm:ss.sTZD
    >
    >to
    >
    >yyyy-MM-dd HH:mm:ss


    Check out Calendar, GregorianCalendar and SimpleDateFormat classes

    >thanks for any tips


    Your welcome.

    --
    now with more cowbell
     
    Bryce, Sep 24, 2004
    #2
    1. Advertising

  3. Jack wrote:
    > How to convert:
    > yyyy-MM-ddThh:mm:ss.sTZD
    > to
    > yyyy-MM-dd HH:mm:ss
    > ?
    > thanks for any tips
    >


    Probably like this:
    String s1 = ....;
    Date d = (new SimpleDateFormat("yyyy-MM-ddThh:mm:ss.sTZD")).parse(s1);
    String s2 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(d);

    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, Sep 24, 2004
    #3
  4. "Jack" <> schreef in bericht
    news:19X4d.241994$...
    >
    > How to convert:
    >
    > yyyy-MM-ddThh:mm:ss.sTZD
    >
    > to
    >
    > yyyy-MM-dd HH:mm:ss


    dateStr.substring(0, dateStr.lastIndexOf('.'))
     
    Boudewijn Dijkstra, Sep 24, 2004
    #4
  5. Jack

    P.Hill Guest

    Boudewijn Dijkstra wrote:

    >>How to convert:
    >>yyyy-MM-ddThh:mm:ss.sTZD
    >>to
    >>yyyy-MM-dd HH:mm:ss

    >
    > dateStr.substring(0, dateStr.lastIndexOf('.'))


    You forget to change the 'T' to a space and the hours of the day
    to AM/PM hours.

    -Paul
     
    P.Hill, Sep 27, 2004
    #5
  6. "P.Hill" <> schreef in bericht
    news:cj9jhd$ppb$...
    > Boudewijn Dijkstra wrote:
    >
    > >>How to convert:
    > >>yyyy-MM-ddThh:mm:ss.sTZD
    > >>to
    > >>yyyy-MM-dd HH:mm:ss

    > >
    > > dateStr.substring(0, dateStr.lastIndexOf('.'))

    >
    > You forget to change the 'T' to a space and the hours of the day
    > to AM/PM hours.


    OK, OK. How about:
    dateStr.substring(0, dateStr.indexOf('T')) + ' ' +
    (Integer.parseInt(dateStr.subString(dateStr.indexOf('T') + 1,
    dateStr.indexOf('T') + 3) % 12) + dateStr.substring(dateStr.indexOf(':'),
    dateStr.lastIndexOf('.'))
     
    Boudewijn Dijkstra, Sep 28, 2004
    #6
  7. Jack

    P.Hill Guest

    Boudewijn Dijkstra wrote:
    [...]
    >>>>yyyy-MM-ddThh:mm:ss.sTZD
    >>>>to
    >>>>yyyy-MM-dd HH:mm:ss

    [...]
    >>
    >>You forget to change the 'T' to a space and the hours of the day
    >>to AM/PM hours.

    >
    > OK, OK. How about:
    > dateStr.substring(0, dateStr.indexOf('T')) + ' ' +
    > (Integer.parseInt(dateStr.subString(dateStr.indexOf('T') + 1,
    > dateStr.indexOf('T') + 3) % 12) + dateStr.substring(dateStr.indexOf(':'),
    > dateStr.lastIndexOf('.'))

    LOL

    I just hope you never get a single digit hour w/o a leading space in the hours
    position.
    yyyy-MM-ddThh:mm:ss.sTZD
    2001-9-11T8:45:00.000EDT

    It just might be easier to use SimpleDateFormat. Just to check the SDF can
    handle one digit hours, I tried the code posted by Thomas and found it contained
    a few errors in the format (now he did say 'Probably like this', so I won't hold
    it against him).

    String s1 = "2001-9-11T8:45:00.000EDT";
    Date d = (new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSzzz")).parse(s1);
    String s2 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(d);
    System.out.println( s2 );

    The formatting is minor compared to a larger problem. What happens if the
    original string is Middle European Time and the VM is running in North American
    Pacific Time?
    The results may not be what is expected. The original OP should consider this.
    Now, Boudewijn didn't do too bad for typing it in without testing it (I could
    tell since substring does not have a capital S), but I would suggest the
    judicious use of subexpressions for clarity.

    int t = dateStr.indexOf('T');
    int nextColon = dateStr.indexOf(":", t);
    int hour = Integer.parseInt( dateStr.substring(t+1, nextColon )) % 12;
    String s3 = dateStr.substring(0, t) + ' ' +
    hour + dateStr.substring( nextColon, dateStr.lastIndexOf('.'));
    System.out.println( s3 );

    -Paul
     
    P.Hill, Sep 28, 2004
    #7
  8. "P.Hill" <> schreef in bericht
    news:cjc4ud$ol7$...
    > Boudewijn Dijkstra wrote:
    > [...]
    > >>>>yyyy-MM-ddThh:mm:ss.sTZD
    > >>>>to
    > >>>>yyyy-MM-dd HH:mm:ss

    > [...]
    > >>
    > >>You forget to change the 'T' to a space and the hours of the day
    > >>to AM/PM hours.

    > >
    > > OK, OK. How about:
    > > dateStr.substring(0, dateStr.indexOf('T')) + ' ' +
    > > (Integer.parseInt(dateStr.subString(dateStr.indexOf('T') + 1,
    > > dateStr.indexOf('T') + 3) % 12) + dateStr.substring(dateStr.indexOf
    > > (':'),> dateStr.lastIndexOf('.'))

    >
    > LOL
    >
    > I just hope you never get a single digit hour w/o a leading space in
    > the hours position.
    > yyyy-MM-ddThh:mm:ss.sTZD
    > 2001-9-11T8:45:00.000EDT


    Then it would be "yyyy-M-ddTh:mm:ss.sssTZD", I guess.

    > It just might be easier to use SimpleDateFormat. Just to check the
    > SDF can handle one digit hours, I tried the code posted by Thomas and
    > found it contained a few errors in the format (now he did say
    > 'Probably like this', so I won't hold it against him).
    >
    > String s1 = "2001-9-11T8:45:00.000EDT";
    > Date d = (new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSzzz")).parse
    > (s1);
    > String s2 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(d);
    > System.out.println( s2 );
    >
    > The formatting is minor compared to a larger problem. What happens if
    > the original string is Middle European Time and the VM is running in
    > North American Pacific Time?


    Isn't DateFormat.setTimeZone("MET") supposed to handle this?

    > The results may not be what is expected. The original OP should
    > consider this. Now, Boudewijn didn't do too bad for typing it in
    > without testing it (I could tell since substring does not have a
    > capital S), but I would suggest the judicious use of subexpressions
    > for clarity.
    >
    > int t = dateStr.indexOf('T');
    > int nextColon = dateStr.indexOf(":", t);
    > int hour = Integer.parseInt( dateStr.substring(t+1,
    > nextColon )) % 12;
    > String s3 = dateStr.substring(0, t) + ' ' +
    > hour + dateStr.substring( nextColon,
    > dateStr.lastIndexOf('.'));
    > System.out.println( s3 );


    Looks good. One question: why did you obtain the 'T' and ':' indices
    differently?
     
    Boudewijn Dijkstra, Sep 30, 2004
    #8
  9. Jack

    P.Hill Guest

    Boudewijn Dijkstra wrote:

    >>I just hope you never get a single digit hour w/o a leading space in
    >>the hours position.
    >>yyyy-MM-ddThh:mm:ss.sTZD
    >>2001-9-11T8:45:00.000EDT

    >
    > Then it would be "yyyy-M-ddTh:mm:ss.sssTZD", I guess.


    FWIW, The ending should be zzz for timezone formating info in a real
    SimpleDateFormat format string.

    >>
    >>The formatting is minor compared to a larger problem. What happens if
    >>the original string is Middle European Time and the VM is running in
    >>North American Pacific Time?

    >
    > Isn't DateFormat.setTimeZone("MET") supposed to handle this?


    Yes, but the original code didn't include the explicit setting of the TZ.

    >> int t = dateStr.indexOf('T');
    >> int nextColon = dateStr.indexOf(":", t);

    [...]
    >
    > Looks good. One question: why did you obtain the 'T' and ':' indices
    > differently?


    I went for the colon after the 'T' which is actually the first colon, so
    yes I could have just asked for the 1st ':'. Could I say I did it 'cause
    starting from the T is faster? If I did I would be making excuses; I did
    it because I was thinking of the 1st colon after the T.

    -Paul
     
    P.Hill, Sep 30, 2004
    #9
    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. Matt
    Replies:
    1
    Views:
    631
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,332
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    549
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    745
    Richard Heathfield
    Nov 8, 2003
  5. Sanjeeb
    Replies:
    2
    Views:
    192
Loading...

Share This Page