【问题标题】:java.text.ParseException: Unparseable date Error on using Clock.systemUTC() [duplicate]java.text.ParseException:使用 Clock.systemUTC() 时出现无法解析的日期错误 [重复]
【发布时间】:2021-09-01 22:28:23
【问题描述】:

我在解析日期时遇到解析错误

java.text.ParseException:无法解析的日期:“2021-06-17T05:49:41.174Z” 无法解析的日期:“2021-06-17T05:49:41.174Z”

我的代码是这样的

private static String generateAndValidate(int count) {
        Clock clock = Clock.systemUTC(); 
        String clockTime=clock.instant().toString();
        String result=clockTime;
        SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ",Locale.ENGLISH);
        try {
            output.parse(clockTime);
        } catch (ParseException e) {
            System.out.println("process date parse error. Going for retry.");
            
        }
        return result;
    }

还尝试在这里对值进行硬编码

SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ",Locale.ENGLISH);
        try {
            output.parse("2021-06-17T05:49:41.174Z");
        } catch (ParseException e) {
            System.out.println("process date parse error. Going for retry.");
            
        }

可能是什么问题?

【问题讨论】:

  • 既然你可以使用java.time,现代Java日期和时间API,不要再混入旧的和麻烦的SImpleDateFormat类。 Clock 是 java.time 的一部分。另外,您要实现的更大目标是什么?我们或许可以帮助您找到更好的方法。
  • 你为什么还用SimpleDateFormat?它已经过时了很长时间。请改用DateTimeFormatter。请参阅 deHaar 的回答。
  • 我犹豫地将 Parse Date String in Java [duplicate] 列为原始问题。请注意那里接受的答案不正确。 The other answer, by achAmháin, 很好。

标签: java date


【解决方案1】:

编辑:您的代码失败的原因在于@GS3 给出的答案!

我的回答提供了通常被认为是最新的替代方案:

我不建议在这里使用java.text.SimpleDateFormat,因为当您使用java.time.Clock 通过现代API 接收时间时,您涉及到一个非常古老且实际上已经过时的API。

一个好的做法是使用java.time.format.DateTimeFormatters 进行解析,但我认为您甚至可以跳过时钟并使用OffsetDateTime.now(ZoneOffset.UTC)

但是,这段代码肯定会解析您第一行生成的String

public static void main(String[] args) {
    // your first two lines
    Clock clock = Clock.systemUTC();
    String clockTime = clock.instant().toString();
    // a hint to the problem
    System.out.println(clockTime + " <--- 6 fractions of second");
    // how to parse a String like that in plain java.time
    OffsetDateTime odt = OffsetDateTime.parse(clockTime);
    System.out.println(odt.format(
                            DateTimeFormatter.ISO_OFFSET_DATE_TIME
                            )
    );
}

其输出将如下所示(显然具有不同的值):

2021-06-17T06:34:55.490370Z <--- 6 fractions of second
2021-06-17T06:34:55.49037Z

使用DateTimeFormatter.ISO_OFFSET_DATE_TIME 的输出只是一种选择,您仍然可以使用DateTimeFormatter.ofPattern(yourPatternString)DateTimeFormatterBuilder 定义自己的模式,以处理可选部分或其他内置格式化程序之一。

如果您只想获取当前时刻并将其存储在某个日期时间类中,您可以使用 now() 方法 java.time 中的日期时间类:

OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);

这里看起来很合适,但也有ZonedDateTime

看看java.time...

【讨论】:

    【解决方案2】:

    在 SimpleDateFormat 中,Z 表示 RFC-822 时区语法的 restricted subsetInstant::toString() 提供 ISO-8601 格式的时间戳。您可以使用 X 而不是 Z 来解决此问题。

    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX",Locale.ENGLISH);
    

    【讨论】:

    • 这绝对解决了这里的特定问题,所以 +1,但 OP 应该停止使用SimpleDateFormat
    猜你喜欢
    • 2016-03-18
    • 2021-07-05
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多