基本数据格式在Java中转换

问题描述 投票:0回答:1

我有一个具有数据选择器的JSP,输出以dd-MM-yyyy格式返回数据字符串。

现在我希望输出的日期时间格式完全正确-> 2019-01-01T01:01:59Z。

我想这是从简单的日期格式到日期时间格式。我尝试了它的抛出错误。这是我的代码。 formatedate2失败。

DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyy", Locale.ENGLISH);
    LocalDate date = LocalDate.parse("2018-04-10T04:00:00.000Z", inputFormatter);
    String formattedDate = outputFormatter.format(date);
    LocalDate date2 = LocalDate.parse("12-10-2019", outputFormatter);
    String formattedDate2 = inputFormatter.format(date2);


    System.out.println(formattedDate2); 

有人可以引导我完成这项工作。感谢您的大力帮助

java simpledateformat date-format
1个回答
0
投票

这将使用您的LocalDate,并使用00:00的LocalTime.MIN将其转换为LocalDateTime。然后,我们使用atZone和UTC的ZoneId将LocalDateTime转换为ZonedDateTime。然后,我们使用format()将ZonedDateTime格式化为所需的格式。我希望这有帮助。

        LocalDate date = LocalDate.parse("2018-04-10");

        LocalDateTime localDateTime = date.atStartOfDay();

        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("UTC"));

        System.out.println(zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")));

输出:2018-04-10T00:00:00.000Z

© www.soinside.com 2019 - 2024. All rights reserved.