Help with date conversion

T

Thomas Fritsch

Jack said:
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);
 
B

Boudewijn Dijkstra

P.Hill said:
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('.'))
 
P

P.Hill

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
 
B

Boudewijn Dijkstra

P.Hill said:
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?
 
P

P.Hill

Boudewijn said:
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.
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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top