简体   繁体   English

以正确的格式将 LocalDate 解析为 ZonedDateTime

[英]Parsing LocalDate to ZonedDateTime in correct format

Given:鉴于:

public static void main(String[] args) {

   String dateString = "2018-07-30T13:36:17.820";

    DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
            .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

    LocalDate date = LocalDate.parse(dateString, DATE_TIME_FORMATTER);

    ZonedDateTime zonedDateTime = date.atStartOfDay((ZoneOffset.UTC));

    System.out.println(zonedDateTime);
}

And output:和 output:

2018-07-30T00:00Z

...what is the pattern to print seconds? ...打印秒的模式是什么? Stupid question no doubt but driving me a little nuts毫无疑问是愚蠢的问题,但让我有点发疯

I need:我需要:

2018-07-30T00:00:00Z

I changed java.time.LocalDate to java.time.LocalDateTime , you need it if you want to show also the seconds.我将java.time.LocalDate更改为java.time.LocalDateTime ,如果您还想显示秒数,则需要它。

package com.test;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateFormatter {

    public static void main(String[] args) {

        String dateString = "2018-07-30T13:36:17.820";

        DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
                .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

        LocalDateTime date = LocalDateTime.parse(dateString, DATE_TIME_FORMATTER);

        ZonedDateTime zonedDateTime = date.atZone(ZoneOffset.UTC);


        System.out.println(zonedDateTime);
    }
}

Output is: Output 是:

2018-07-30T13:36:17.820Z

LocalDate will keep just date. LocalDate 将保持日期。 You need to parse LocalDateTime and convert to ZonedDateTime and you will have seconds as you expect.您需要解析 LocalDateTime 并转换为 ZonedDateTime 并且您将拥有预期的秒数。

    var dateString = "2018-07-30T13:36:17.820";
    var format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
    var localDate = LocalDateTime.parse(dateString, format);
    var zone = ZoneId.of( "America/Montreal" );
    var zonedDateTime = localDate.atZone(zone);
    System.out.println(zonedDateTime);

You will have to go a few steps:您将不得不 go 几个步骤:

  • parse the String to a LocalDateTime because it contains date and time of dayString解析为LocalDateTime ,因为它包含日期和时间
  • extract the date only仅提取日期
  • create a ZonedDateTime out of that by adding the start of day ( LocalTime.MIN = 00:00:00 ) and a ZoneOffset.UTC通过添加一天的开始( LocalTime.MIN = 00:00:00 )和ZoneOffset.UTC ZonedDateTime

This code may do:此代码可能会:

public static void main(String[] args) {
    String dateString = "2018-07-30T13:36:17.820";
    // parse a LocalDateTime
    LocalDateTime localDateTime = LocalDateTime.parse(dateString);
    // extract the date part
    LocalDate localDate = localDateTime.toLocalDate();
    // make it a ZonedDateTime by applying a ZoneId
    ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, LocalTime.MIN, ZoneOffset.UTC);
    // print the result
    System.out.println(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}

Output is Output 是

2018-07-30T00:00:00Z

There are several ways to do it, this is just one of them and it just slightly differs from most of the other answers (and comments:-) ).有几种方法可以做到这一点,这只是其中之一,它与大多数其他答案(和评论:-) 略有不同。

tl;dr tl;博士

You have used the wrong things in the wrong places.你在错误的地方使用了错误的东西。

  1. You do not need a DateTimeFormatter explicitly in order to parse 2018-07-30T13:36:17.820 because it's already in ISO 8601 format which is also the default format used by LocalDateTime#parse .您不需要DateTimeFormatter明确地解析2018-07-30T13:36:17.820因为它已经采用ISO 8601格式,这也是LocalDateTime#parse使用的默认格式。 Moreover, this string has date and time instead of just date;此外,这个字符串有日期和时间,而不仅仅是日期; therefore, it makes more sense to parse it into LocalDateTime instead of LocalDate .因此,将其解析为LocalDateTime而不是LocalDate更有意义。 You can always get LocalDate from LocalDateTime using LocalDateTime#toLocalDate .您始终可以使用LocalDateTime#toLocalDateLocalDateTime获取LocalDate
  2. The ZonedDateTime#toString uses the LocalDateTime#toString which in turn uses LocalTime#toString for the time part which omits second and fraction-of-second if they are zero. ZonedDateTime#toString使用LocalDateTime#toString反过来使用LocalTime#toString作为时间部分,如果它们为零,则省略秒和秒的分数。 If you need a string with zero second and fraction-of-second, you will need to use a DateTimeFormatter .如果您需要零秒和小数秒的字符串,则需要使用DateTimeFormatter

Demo:演示:

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String args[]) {
        String dateString = "2018-07-30T13:36:17.820";

        LocalDateTime localDateTime = LocalDateTime.parse(dateString);// You do not need a DateTimeFormatter here

        ZonedDateTime zonedDateTime = localDateTime.toLocalDate().atStartOfDay(ZoneOffset.UTC);

        // Print zonedDateTime.toString()
        System.out.println(zonedDateTime);

        // Custom format
        final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
        System.out.println(DATE_TIME_FORMATTER.format(zonedDateTime));
    }
}

Output: Output:

2018-07-30T00:00Z
2018-07-30T00:00:00.000

Learn more about the modern date-time API from Trail: Date Time .Trail: Date Time了解有关现代日期时间 API 的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM