开发者

How to convert date/time strings into Java Calendar objects? [duplicate]

开发者 https://www.devze.com 2023-04-09 04:44 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How to convert a date String to a Date or Calendar object?
This question already has answers here: Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消