【问题标题】:Java 8 date/time: instant, could not be parsed at index 19Java 8 日期/时间:即时,无法在索引 19 处解析
【发布时间】:2017-06-29 07:19:50
【问题描述】:

我有以下代码:

String dateInString = "2016-09-18T12:17:21:000Z";
Instant instant = Instant.parse(dateInString);

ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Europe/Kiev"));
System.out.println(zonedDateTime);

它给了我以下异常:

线程“主”java.time.format.DateTimeParseException 中的异常: 无法在索引 19 处解析文本“2016-09-18T12:17:21:000Z” java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) 在 java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) 在 java.time.Instant.parse(Instant.java:395) 在 core.domain.converters.TestDateTime.main(TestDateTime.java:10)

当我将最后一个冒号改为句号时:

String dateInString = "2016-09-18T12:17:21.000Z";

…然后执行顺利:

2016-09-18T15:17:21+03:00[欧洲/基辅]

所以,问题是 - 如何使用 InstantDateTimeFormatter 解析日期?

【问题讨论】:

    标签: java date parsing time java.time.instant


    【解决方案1】:

    “问题”是毫秒前的冒号,是非标准的(标准是小数点)。

    要使其工作,您必须为您的自定义格式构建一个自定义DateTimeFormatter

    String dateInString = "2016-09-18T12:17:21:000Z";
    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
        .append(DateTimeFormatter.ISO_DATE_TIME)
        .appendLiteral(':')
        .appendFraction(ChronoField.MILLI_OF_SECOND, 3, 3, false)
        .appendLiteral('Z')
        .toFormatter();
    LocalDateTime instant = LocalDateTime.parse(dateInString, formatter);
    ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Europe/Kiev"));
    System.out.println(zonedDateTime);
    

    这段代码的输出:

    2016-09-18T12:17:21+03:00[Europe/Kiev]
    

    如果你的日期时间文字有一个点而不是最后一个冒号,事情会简单得多。

    【讨论】:

    • 实际上标准是逗号句号(句号),首选逗号。参见 ISO 8601:2004 第三版 2004-12-01,“4.2.2.4 小数表示”部分:……小数部分应除以 ISO 31 中指定的小数符号-0,即逗号 [,] 或句号 [.]。其中,逗号是首选符号。 …
    • @basil 可能适用于 ISO,但不适用于 JDK,which uses the decimal point
    • 关于我的问题的任何想法:stackoverflow.com/questions/57174218/…
    【解决方案2】:

    使用SimpleDateFormat

    String dateInString = "2016-09-18T12:17:21:000Z";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS");
    Instant instant = sdf.parse(dateInString).toInstant();
    ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Europe/Kiev"));
    System.out.println(zonedDateTime);
    

    2016-09-18T19:17:21+03:00[欧洲/基辅]

    【讨论】:

      【解决方案3】:
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
      
      String date = "16/08/2016";
      
      //convert String to LocalDate
      LocalDate localDate = LocalDate.parse(date, formatter);
      

      如果String的格式类似于ISO_LOCAL_DATE,则可以直接解析String,无需转换。

      package com.mkyong.java8.date;
      
      import java.time.LocalDate;
      
      public class TestNewDate1 {
      
          public static void main(String[] argv) {
      
              String date = "2016-08-16";
      
              //default, ISO_LOCAL_DATE
              LocalDate localDate = LocalDate.parse(date);
      
              System.out.println(localDate);
      
          }
      
      }
      

      看看这个网站 Site here

      【讨论】:

      • 时间分量呢?
      猜你喜欢
      • 2021-01-15
      • 2015-06-26
      • 2017-09-26
      • 1970-01-01
      • 2021-02-27
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多