开发者

How can I convert UTC to a DateTime?

开发者 https://www.devze.com 2023-03-07 22:11 出处:网络
I have the following date: 2011-05-24T11:40:41Z How can I convert this into a Joda DateTime? EDIT: Here is my code that does not compile:

I have the following date:

2011-05-24T11:40:41Z

How can I convert this into a Joda DateTime?

EDIT: Here is my code that does not compile:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 
                        Date date = df.parse(aMessage.getString("updated_at"));
                        org.joda.time.DateTime dt = new DateTime(date);

Errors:

The method parse(String) is undefined for the type DateFormat   
Type mismatch: cannot convert from SimpleDateFo开发者_如何学JAVArmat to DateFormat


From Joda's user guide:

All printing and parsing is performed using a DateTimeFormatter object.

You should be able to just use the ISODateTimeFormat factory directly in this instance:

String inputDateString = "2011-05-24T11:40:41Z";

DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
DateTime result = fmt.parseDateTime(inputDateString);


Use DatFormat. It has a parse() method you could use.

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    df.parse("2011-05-24T11:40:41Z");
    org.joda.time.DateTime dt = new DateTime(date);
0

精彩评论

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