J
Jack
How to convert:
yyyy-MM-ddThh:mm:ss.sTZD
to
yyyy-MM-dd HH:mm:ss
?
thanks for any tips
yyyy-MM-ddThh:mm:ss.sTZD
to
yyyy-MM-dd HH:mm:ss
?
thanks for any tips
How to convert:
yyyy-MM-ddThh:mm:ss.sTZD
to
yyyy-MM-dd HH:mm:ss
thanks for any tips
Jack said:How to convert:
yyyy-MM-ddThh:mm:ss.sTZD
to
yyyy-MM-dd HH:mm:ss
?
thanks for any tips
Jack said:How to convert:
yyyy-MM-ddThh:mm:ss.sTZD
to
yyyy-MM-dd HH:mm:ss
Boudewijn said:dateStr.substring(0, dateStr.lastIndexOf('.'))
P.Hill said:You forget to change the 'T' to a space and the hours of the day
to AM/PM hours.
LOL[...]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('.'))
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
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 );
Boudewijn said:Then it would be "yyyy-M-ddTh:mm:ss.sssTZD", I guess.
Isn't DateFormat.setTimeZone("MET") supposed to handle this?
[...]int t = dateStr.indexOf('T');
int nextColon = dateStr.indexOf(":", t);
Looks good. One question: why did you obtain the 'T' and ':' indices
differently?
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.