views:

79

answers:

3

Hi,

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?

+2  A: 

SimpleDateFormat

  public static void main(String[] args) {
 try {    String str_date="11-June-07";
         DateFormat formatter ; 
     Date date ; 
          formatter = new SimpleDateFormat("dd-MMM-yy");
              date = (Date)formatter.parse(str_date);    
               System.out.println("Today is " +date );
    } catch (ParseException e)
    {System.out.println("Exception :"+e);    }    

   }  

JodaTime

import org.joda.time.format.*;
import org.joda.time.*;

...    

String dateString = "2009-04-17 10:41:33";

// parse the string
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateTime = formatter.parseDateTime(dateString);

// add two hours
dateTime = dateTime.plusHours(2); // easier than mucking about with Calendar and constants

System.out.println(dateTime);
org.life.java
+3  A: 

Well, that's what java.text.DateFormat is for (and particularly its SimpleDateFormat subclass) - but personally I would suggest that you use Joda Time instead.

In particular Joda Time's DateTimeFormatter class is thread-safe, unlike SimpleDateFormat - so you can create a single instance with the appropriate pattern, and use it from any thread. Additionally, the DateTimeFormat class acts as a factory with lots of preset patterns in ISODateFormat. Oh, and controlling the time zone etc is rather better with Joda Time.

Finally, Joda Time is simply a better date and time API. It's not perfect, but it's much better than the built-in Date and Calendar support in Java.

EDIT: Trying to parse your sample string, I'm having trouble with the "BST" bit... partly because that's not really a full time zone (it's just the DST part of the Europe/London time zone) and partly because I can't quite get Joda Time to do what I want... it looks like in this one case, SimpleDateFormat wins out :(

Jon Skeet
Amazing, I've never seen JodaTime be deficient compared to JDK date handling! Thanks for your complete answer, very helpful.
Chris Knight
Thx for the tip on yoda - good to know it exists!
david
+7  A: 

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.

Bozho
should be `HH` not `hh`.
dogbane
sure, thanks, fixed.
Bozho
@Bozho: Unfortunately it looks like Joda Time doesn't handle parsing the time zone :(
Jon Skeet
@Jon Skeet - very odd. The JodaTime Formatter fails to parse even the string generated by the very same formatter. Might be a bug?
Bozho
@Bozho: No, I think it's a known limitation - the docs specify various things about time zones not being parseable. Something I hope to fix in Noda Time, definitely... if possible, anyway :)
Jon Skeet
@Bozho: Ah, thanks, I knew there must be something like this. I've used SimpleDateFormat lots of times for formating, but didn't realize it did the converse as well.
Chris Knight