简体   繁体   中英

java org.joda.time.DateTime illegal pattern

This is my string

2011-01-01T00:00:00.000Z

I am changing it to date time object using org.joda.time.DateTime

This is my code

DateTimeFormatter formatter = DateTimeFormat
                .forPattern("yyyy-MM-ddTHH:mm:ss.000Z");
        DateTime dt = formatter.parseDateTime("2011-01-01T00:00:00.000Z");

I got exception

Illegal pattern component: T

So the T and .000Z are always there, how can I format ?

You need to escape the T

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.000Z");
DateTime dt = formatter.parseDateTime("2011-01-01T00:00:00.000Z");
System.out.println(dt);

Also, not too sure why you want to enforce the millis to be 000 . If they are always 000 , you probably should not include them at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM