0

Possible Duplicate:
How to convert a date String to a Date or Calendar object?

I'm in a pinch here...

I have a series of date/time strings formatted like this:"9-29-2011 9:05 PM PDT" and I need to convert this string into a java Calendar object. Once I have this Calendar object it must represent exactly this date and time (including the AM or PM). Please, what is the best way to accomplish this?

Community
  • 1
  • 1
GimmeShelter
  • 77
  • 1
  • 5

2 Answers2

0

Use DateFormat to parse the String. Pass the Date to the Calendar.setTime() method.

John B
  • 32,493
  • 6
  • 77
  • 98
-1

Check out http://joda-time.sourceforge.net/ . Joda Time makes a lot of tricky date/time operations simple!

You will probably want to construct a custom formatter: http://joda-time.sourceforge.net/userguide.html#Input_and_Output

DateTimeFormat.forPattern("dd-MM-yyyy hh:mm a z"); should be close. You can then convert the Joda DateTime to Date or to a Calendar.

Freiheit
  • 8,408
  • 6
  • 59
  • 101