Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java DateTime format for this one

Tags:

java

datetime

What is the Java DateTime format for this one?

Mon Nov 26 13:57:03 SGT 2012

I want to convert this string to Date and convert it to another format like "yyyy-MM-dd HH:mm:ss".

To convert from date to string is not hard.

Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

But I find no valid format to convert "Mon Nov 26 13:57:03 SGT 2012" to become date format...

=====

found solution:

DateFormat oldDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Format newDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date oldDate = oldDateFormat.parse(oldTimeString);
String newDateString = newDateFormat.format(oldDate);
like image 366
lannyboy Avatar asked Feb 21 '26 02:02

lannyboy


1 Answers

This will work, EEE MMM dd HH:mm:ss z yyyy

You can find examples in the javadoc of SimpleDateFormat. See http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

like image 57
Reddy Avatar answered Feb 22 '26 15:02

Reddy