我有一个Java程序.在我的Java程序中,输入将是不同格式的日期字符串,如下所示

a) 2021-10-09T08:00:01.111Z ( with millisecond)
b) 2021-10-09T08:00:01Z ( without milliseconds)
c) 2021-10-09T08:00Z ( no seconds )
d) 2021-10-09T08Z ( no minutes ) 

目前,我使用的是内置的日期格式化程序DateTimeFormatter.ISO_OFFSET_DATE_TIME.但是,当我运行我的样例代码片段程序时,它失败了

以下是我的示 routine 序

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class Tester2 {

    public static void main(String[] args) {
        
        OffsetDateTime t1 = OffsetDateTime.parse("2021-10-09T08:00:01.111Z");;
        System.out.println(t1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
        
        OffsetDateTime t2 = OffsetDateTime.parse("2021-10-09T08:00:01Z");;
        System.out.println(t2.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
        
        OffsetDateTime t3 = OffsetDateTime.parse("2021-10-09T08:00Z");;
        System.out.println(t3.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
   
        OffsetDateTime t4 = OffsetDateTime.parse("2021-10-09T08Z");;
        System.out.println(t4.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
    }

}

然而,当我运行我的程序时,它失败了,输出如下.

2021-10-09T08:00:01.111Z
2021-10-09T08:00:01Z
2021-10-09T08:00:00Z
Exception in thread "main" java.time.format.DateTimeParseException: Text '2021-10-09T08Z' could not be parsed at index 13
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:404)
    at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:389)
    at test.Tester2.main(Tester2.java:20)

以下是我想要的输出

2021-10-09T08:00:01.111Z
2021-10-09T08:00:01Z
2021-10-09T08:00:00Z
2021-10-09T08:00:00Z

有没有什么Java内置的数据格式化程序可以帮助我实现想要的输出.如果没有这样的日期格式化程序,你能帮我写一个新的日期格式化程序吗?欢迎另一种解决方案.

推荐答案

DataTimeFormatter支持包含[]个字符的可选解析模式.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH[:mm[:ss[.SSS]]]X");

    OffsetDateTime t1 = OffsetDateTime.parse("2021-10-09T08:00:01.111Z", formatter);
    System.out.println(t1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

    OffsetDateTime t2 = OffsetDateTime.parse("2021-10-09T08:00:01Z", formatter);
    System.out.println(t2.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

    OffsetDateTime t3 = OffsetDateTime.parse("2021-10-09T08:00Z", formatter);
    System.out.println(t3.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

    OffsetDateTime t4 = OffsetDateTime.parse("2021-10-09T08Z", formatter);
    System.out.println(t4.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

输出结果是您所要求的:

2021-10-09T08:00:01.111Z
2021-10-09T08:00:01Z
2021-10-09T08:00:00Z
2021-10-09T08:00:00Z

Java相关问答推荐

Kubernetes的Java客户端检索状态.处于终止状态的Pod的阶段';正在运行';

什么是Java原子属性的正确getter和setter

解释左移在Java中的工作原理

PDFBox未加载内容

带错误BER验证的itext8签名返回pdf

从ActiveMQ Classic迁移到ActiveMQ Artemis需要进行哪些客户端更改?

类型集合的Jackson JsonNode:类型引用的对象读取器应该是Singleton吗?

由于在生成器模式中使用泛型,lambda表达式中的返回类型错误

为什么在下面的Java泛型方法中没有类型限制?

通过Java列表中的某些字段搜索值

使用SWIG将C++自定义单元类型转换为基本Java类型

我的代码是线程安全的吗?[Java、CAS、转账]

Win32函数的JNA绑定DwmGetColorizationColor返回E_INVALIDARG错误

JavaFX,GridPane:在GridPane的列中生成元素将保持所有列的宽度

放置在变量中的Java成员引用不相等

为什么child-pom会创建一个新版本

在JPanel上使用GridBagLayout并将JButton放在里面时出现问题

如何在Java中立即显示ProgressBar对话框?

当小数位数已知时,BigDecimal 除法有缺点吗?

将 JSF 2.3 应用程序部署到 Tomcat 9.0.80 时出现异常