Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

parsing - Problem with parse a LocalDateTime using java 8

I have this code, it's a simple string that I want to parse it to a LocalDateTime

    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeFormatterBuilder;

    public class DateClass {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {

            String dateRaw = "2019-05-03 7:05:03";        

            DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("uuuu-mm-dd HH:mm:ss").toFormatter(); 

            LocalDateTime date= LocalDateTime.parse(dateRaw, dtf);
            System.out.println(date.toString());
        }

    }

And when y runing, I have the next error:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2019-05-03 7:05:03' could not be parsed at index 11
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at lectordeachvio.DateClass.main(DateClass.java:18)

what I doing wrong? and why has a fault with de space????

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd H:mm:ss");

With this change your program outputs:

2019-05-03T07:05:03

  • A single format pattern letter H will match hour of day in either 1 or two digits. That is, it will accept 7, 07, 13, etc. Two HH on the other hand requires two digits like 07 or 13, so 7 alone cannot be parsed. This was the reason for the exception that you got.
  • Index 11 of your string is not where the space is. It is where the 7 is. Indices are 0-based.
  • As others have mentioned you also need to use uppercase MM for month number. Lowercase mm is for minute of hour.
  • As an aside you don’t necessarily need a DateTimeFormatterBuilder for this case. DateTimeFormatter.ofPattern works OK.

Just out of curiosity, if your formatter is for parsing only, you may omit all repetitions of pattern letters. This works too:

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s");

Normally we would not want this, though. We’d prefer to validate that there are two digits for minutes and seconds, often also for month and day of month. Putting two pattern letters accomplishes that.

Partly related question about mm in the format pattern: Convert LocalDate in DD/MM/YYYY LocalDate [duplicate].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...