2

How to convert the date Tue Nov 13 14:35:04 +0000 2012 String format to date in Java? I know of Date.parse(String) but I don't know which format I should use for the date. Do I have to modify the string so that it can be parsed into date, and if yes then how?

andrewdotn
  • 32,721
  • 10
  • 101
  • 130
user1268398
  • 35
  • 2
  • 6
  • I think the format is `FULL` [my source](http://docs.oracle.com/javase/tutorial/i18n/format/dateFormat.html). any way, what have you tried? – elyashiv Nov 22 '12 at 16:04
  • The JavaDoc for [`Date.parse(String)`](http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#parse(java.lang.String)) suggests using `SimpleDateFormat`, as in the answer below. – andrewdotn Nov 22 '12 at 16:13
  • [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573) – Arvind Kumar Avinash Jun 27 '21 at 12:12

2 Answers2

6

Use SimpleDateFormat, with a format string of

"E MMM dd HH:mm:ss Z yyyy"

You should explicitly use Locale.US assuming these will definitely use English month/day names. (You don't want to be trying to parse French names just because the default locale is French, for example.)

Also, don't forget that the Date value returned will have no knowledge of the original time zone - it will have the right value for the instant represented in the original text, but don't expect the result of calling toString() to use the same zone - Date.toString() always uses the default time zone.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @NimChimpsky: Your comment would have been considerably more helpful if you'd indicated in what *way* it's incorrect (or just edited the answer). – Jon Skeet Nov 22 '12 at 16:24
0
 SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
 Date test = sdf.parse("Tue Nov 13 14:35:04 +0000 2012");
 System.out.print(test.toString());
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311