使用Java 8解析LocalDateTime的问题 [英] Problem with parse a LocalDateTime using java 8

查看:162
本文介绍了使用Java 8解析LocalDateTime的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,这是一个简单的字符串,我想将其解析为LocalDateTime

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????

推荐答案

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd H:mm:ss");

通过此更改,您的程序输出:

With this change your program outputs:

2019-05-03T07:05:03

2019-05-03T07:05:03

  • 单一格式的图案字母H将以1或两位数字匹配一天中的小时.也就是说,它将接受70713等.另一方面,两个HH需要两个数字,例如0713,因此不能单独解析7.这就是您例外的原因.
  • 字符串的索引11不在空格中.这是7所在的位置.索引从0开始.
  • 就像其他人提到的那样,您还需要使用大写的MM作为月份编号.小写的mm是一小时的分钟.
  • 顺便说一句,在这种情况下,您不一定需要DateTimeFormatterBuilder. DateTimeFormatter.ofPattern正常.
    • 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.

      有关mm的部分相关问题,格式格式为:在DD/MM/YYYY LocalDate [重复] .

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

      这篇关于使用Java 8解析LocalDateTime的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆