Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

java - joda DateTime parser error

I use jodatime to parse date time strings as follows:

    public static void main(String[]args){
        String s ="16-Jul-2009 05:20:18 PDT";
        String patterns = "dd-MMM-yyyy HH:mm:ss z";

            DateTimeFormatter fm = DateTimeFormat.forPattern(patterns);
            DateTime d=fm.parseDateTime(s);
            System.out.println(d);

    }

I get

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "16-Jul-2009 05:20:18 PDT" is malformed at "PDT"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:683)

what's wrong? how to parse the timezone properly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the DateTimeFormat javadoc:

The pattern syntax is mostly compatible with java.text.SimpleDateFormat - time zone names cannot be parsed and a few more symbols are supported. All ASCII letters are reserved as pattern letters, which are defined as follows:

Your best bet is to fall back to SimpleDateFormat and then construct DateTime based on Date#getTime().

String s = "16-Jul-2009 05:20:18 PDT";
String pattern = "dd-MMM-yyyy HH:mm:ss z";
Date date = new SimpleDateFormat(pattern, Locale.ENGLISH).parse(s);
DateTime d = new DateTime(date.getTime());
System.out.println(d);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...