How to convert date/time strings into Java Calendar objects? [duplicate]
Possible Duplicate:
How to convert a date String to a Date or Calendar object?
I'm in a pinch he开发者_运维技巧re...
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?
Use DateFormat to parse the String
. Pass the Date
to the Calendar.setTime()
method.
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.
精彩评论