Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help with date conversion

Reply
Thread Tools

Help with date conversion

 
 
Jack
Guest
Posts: n/a
 
      09-24-2004

How to convert:

yyyy-MM-ddThh:mm:ss.sTZD

to

yyyy-MM-dd HH:mm:ss

?

thanks for any tips


 
Reply With Quote
 
 
 
 
Bryce
Guest
Posts: n/a
 
      09-24-2004
On Fri, 24 Sep 2004 15:35:25 GMT, "Jack"
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      09-24-2004
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

 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      09-24-2004
"Jack" <(E-Mail Removed)> schreef in bericht
news:19X4d.241994$(E-Mail Removed). ..
>
> How to convert:
>
> yyyy-MM-ddThh:mm:ss.sTZD
>
> to
>
> yyyy-MM-dd HH:mm:ss


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


 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      09-27-2004
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

 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      09-28-2004
"P.Hill" <(E-Mail Removed)> schreef in bericht
news:cj9jhd$ppb$(E-Mail Removed)...
> 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.indexO f('T') + 1,
dateStr.indexOf('T') + 3) % 12) + dateStr.substring(dateStr.indexOf(':'),
dateStr.lastIndexOf('.'))


 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      09-28-2004
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.indexO f('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

 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      09-30-2004
"P.Hill" <(E-Mail Removed)> schreef in bericht
news:cjc4ud$ol7$(E-Mail Removed)...
> 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.indexO f('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?


 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      09-30-2004
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

 
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
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Calling date conversion function before binding the date to a daragrid Sanjeeb ASP .Net Web Controls 2 01-26-2004 06:36 AM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments