Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any easy ways to unformat a date?

I've got a bunch of dates in this String format:

String date = "Wed Sep 15 16:31:05 BST 2010";

and I'd like to convert it back to a date or calendar object. Before I go and reinvent the wheel, are there any easy ways of doing this, preferably present in the JDK?

like image 881
Chris Knight Avatar asked Dec 16 '22 21:12

Chris Knight


1 Answers

Using SimpleDateFormat

String format = "EE MMM dd HH:mm:ss zz yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
Date result = sdf.parse(date);

Alternatively, as suggested by Jon Skeet, you can use JodaTime's DateTimeFormat - the pattern should be the same. But it appears that the BDT/BST/BDST timezone aliases are not supported properly by JodaTime.

like image 131
Bozho Avatar answered Dec 19 '22 09:12

Bozho