有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    它非常简单LocalDateTime localDateTime = yourLocalDate.atStartOfDay()

    更新 添加时间戳非常简单:

    ZoneId zoneId = ZoneId.of("America/New_York");
    ZonedDateTime = zdt = localDateTime.atZone(zoneId);
    

    可以作为

    ZonedDateTime zdt = yourLocalDate.atStartOfDay().atZone(ZoneId.of("America/New_York"));
    
  2. # 2 楼答案

    LocalDate上有各种方法可以实现这一点,包括:

    • LocalDate::toDateTimeAtCurrentTime()
    • LocalDate::ToDateTimeAtStarToDay()
    • LocalDate::toDateTime(LocalTime)
    • LocalDate::toDateTime(LocalTime,DateTimeZone)
  3. # 3 楼答案

    我假设您得到了一个字符串,例如2016-01-25,并且您想要一个包含JVM默认时区中一天开始的字符串(这个问题不清楚)。我首先为您想要的格式定义一个格式化程序(它是ISO 8601):

    private static DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSxx");
    

    现在,你的转换是:

        String isoLocalDateString = "2016-01-25";
        LocalDate date = LocalDate.parse(isoLocalDateString);
        ZonedDateTime dateTime = date.atStartOfDay(ZoneId.systemDefault());
        String dateTimeString = dateTime.format(formatter);
        System.out.println(dateTimeString);
    

    在我的时区欧洲/哥本哈根运行时,此示例代码的输出就是您所要求的:

    2016-01-25T00:00:00.000+0100

    在少数情况下,夏季时间(DST)从一天的第一个时刻开始,一天的时间不会是00:00:00.000

    对于使用ISO_LOCAL_DATE进行解析,我们不需要指定格式化程序,因为此格式化程序是LocalDate.parse()的默认设置

    所有这些都表明,通常情况下,您不应该希望将日期从一种字符串格式转换为另一种字符串格式。在程序中,将日期作为LocalDate对象保存。当您获得字符串输入时,将其解析为LocalDate。仅当您需要提供字符串输出时,例如在与另一个系统的数据交换中,才可以按所需格式格式化为字符串

    链接:Wikipedia article: ISO 8601