如何将LocalDate转换为ChronoZonedDateTime?

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

在下面的代码中,我的“ if”比较中出现错误。该消息显示为“ isBefore(java.time.chrono.ChronoZonedDateTime<?>) in ChronoZonedDateTime cannot be applied to (java.time.LocalDate)”。如何将LocalDate转换为ChronoZonedDateTime

LocalDate taxBegin = tax.getBeginAt();

if(contract.getBeginAt().isBefore(taxBegin)){
    //do something
}

我尝试像ChronoZonedDateTime.from(taxBegin)一样包装,但没有用,它给了我[DateTimeException: Unable to obtain ZoneId from TemporalAccessor: 2019-12-01 of type java.time.LocalDat”]

java date datetime converters localdate
1个回答
0
投票

您可以使用atStartOfDay(ZoneId)I.E。

public static ZonedDateTime convertLocalDate(final LocalDate ld) {
    return ld.atStartOfDay(ZoneId.systemDefault());
}

您可以使用ZoneId.systemDefault()ZoneOffset.UTC。文档状态:如果区域ID是ZoneOffset,则结果始终为午夜时间。因此您的代码将是

if (contract.getBeginAt().isBefore(convertLocalDate(taxBegin))) {
    //do something
}

如果要将其转换为特定时间,则应使用taxBegin.atTime(LocalTime).atZone(ZoneId)

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