无法从 TemporalAccessor 获取 Instant:java.time.LocalDateTime 类型的 2023-08-31T20:37:49.005832800

Unable to obtain Instant from TemporalAccessor: 2023-08-31T20:37:49.005832800 of type java.time.LocalDateTime

提问人:gajesh 提问时间:8/31/2023 最后编辑:Anonymousgajesh 更新时间:9/3/2023 访问量:216

问:

我对 Temporal 类使用 parmeterised 方法并将 LocalDateTime 传递给 Temporal 参数,并在 Instant.from(Temporal t) 方法中出现错误,其中 t 在我的情况下为 LocalDateTime 实例化。 这里是程序,

public Class DateTimeUtils {
public static <T extends Temporal> T withZone(T datetime, ZoneId zone) {
        ZonedDateTime zdt = Instant.from(datetime).atZone(zone);    //error Unable to obtain Instant from TemporalAccessor for LocalDateTime
        if (datetime instanceof LocalDate)
            return (T) zdt.toLocalDate();
        else if (datetime instanceof LocalTime)
            return (T) zdt.toLocalTime();
        else if (datetime instanceof LocalDateTime)
            return (T) zdt.toLocalDateTime();
        else if (datetime instanceof Instant)
            return (T) zdt.toInstant();
        else
          return datetime;
}
//getting error while calling DateConvertUtils.withZone function through main
   public static void main(String[] args) {
        System.out.println(LocalDateTime.now());
        System.out.println(DateConvertUtils.withZone(LocalDateTime.now(), ZoneId.systemDefault()));
    }
}

运行此文件时出现以下错误

Exception in thread "main" java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: 2023-08-31T20:37:49.005832800 of type java.time.LocalDateTime
    at java.base/java.time.Instant.from(Instant.java:378)
    at javaapplicationpjl.DateConvertUtils.withZone(DateConvertUtils.java:?)
    at javaapplicationpjl.DateConvertUtils.main(DateConvertUtils.java:?)
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
    at java.base/java.time.LocalDate.get0(LocalDate.java:708)
    at java.base/java.time.LocalDate.getLong(LocalDate.java:687)
    at java.base/java.time.LocalDateTime.getLong(LocalDateTime.java:720)
    at java.base/java.time.Instant.from(Instant.java:373)
    ... 2 more

请您告诉我此错误的原因,因为我们应该能够从 LocalDateTime 对象获取即时时间,并请求您即兴创作我的函数:withZone(..)。这个功能的目的非常明确。 提前致谢。

我正在尝试准备一个通用函数,用于获取具有特定区域的时间库类对象,其中输入和输出类型都是,并且都必须是 Tempal 的实例。

当我在访问 Instant.from(LocalDateTime) 方法时尝试过它时,出现了 **错误。

类型转换 java-time java.time.instant

评论

2赞 aled 9/1/2023
为什么对方法参数使用泛型,然后检查方法内每个可能的类?似乎是一种反模式。
0赞 Anonymous 9/1/2023
欢迎来到 Stack Overflow。你希望你的方法在每种情况下做什么?因为我们应该能够从 LocalDateTime 对象获取即时不,你不应该。这就像要求从梨中吃香蕉一样。或者当没有人知道汇率时,美元中的欧元。这就是您错误的原因。但我们也想告诉你该怎么做,只要我们不知道你想发生什么,我们就不能这样做。
0赞 Anonymous 9/1/2023
您也无法从 或 获取 。因此,似乎没有任何情况可以使您的方法可以像目前一样完成任何实际工作。您可能想再次检查您对这些类的理解以及它们之间的关系?InstantLocalDateLocalTime
0赞 gajesh 9/1/2023
感谢您对我的问题的正确评论。我在这里直接使用泛型而不是 Temporal b'coz,我不希望类型转换来返回值。使用此泛型方法,我将获得与输入类型类似的类型的结果

答:

1赞 Basil Bourque 9/1/2023 #1

TL的;博士

如果不注入时区或偏移量的上下文,则无法进行 from a。InstantLocalDateTime

myLocalDateTime.atZone( … ).toInstant()

方法错误,其中,Instant.from(Temporal t)t instanceof LocalDateTime

LocalDateTime只是一个带有一天中时间的日期。该类没有时区的概念,也没有与 UTC 的偏移量。此类的对象表示时间轴上的某个时刻、特定点。

Instant 确实表示时间轴上的某个时刻、特定点,如与 UTC 的偏移量为 0 小时-分钟-秒一样。

👉🏾 所以你不能从 .InstantLocalDateTime

例如,“2024 年 1 月 23 日中午”,我们无法知道这是东京、图卢兹还是托莱多的中午——三个不同的时刻在时间轴上相隔几个小时。

如果我们知道这是在法国图卢兹的片刻,我们可以执行以下操作来获得.Instant

Instant instant =
    LocalDateTime
        .of( 2024 , 1 , 23 , 12 , 0 , 0 , 0 ) 
        .atZone( ZoneId.of( "Europe/Paris" ) )
        .toInstant() ;

请参阅此代码在 Ideone.com 运行。末尾表示偏移量为零。Z

2024-01-23T11:00:00Z


顺便说一句,我看不出你的目的.更通用的接口(例如)不适用于常见的应用程序编程;它们适用于日期时间类的实现者。DateTimeUtilsTemporal

如果那堂课是试图解决一个特定的问题,我建议你再发布一个关于该主题的问题。我怀疑我们可以找到更好的方法。

1赞 marcinj 9/1/2023 #2

ZonedDateTime zdt = Instant.from(datetime).atZone(zone);错误:无法从 LocalDateTime 的 TemporalAccessor 获取 Instant

此错误是因为不包含有关 UTC 格林威治偏移量的信息,这是将其转换为即时时段所必需的。此异常在它 reches 之前被抛出。 确实实现了,但不支持时区:LocalDateTimeInstant.fromatZoneLocalDateTimeTemporalAccessor

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html

此类不存储或表示时区。相反,它是对日期的描述,用于生日,结合挂钟上看到的当地时间。如果没有其他信息(如偏移量或时区),它无法表示时间轴上的瞬间。

可以使用指定的 ZoneID 将 LocalDateTime 转换为 ZonedDateTime,然后将其转换为 Instant:

public static <T extends Temporal> T withZone(T datetime, ZoneId zone) {
    ZonedDateTime zdt;
    if (datetime instanceof LocalDateTime) {
        zdt = ((LocalDateTime) datetime).atZone(zone);
    else if(datetime instanceof LocalDate) {
        zdt = ((LocalDate) datetime).atStartOfDay(zone);
    } else {
        zdt = Instant.from(datetime).atZone(zone);
    }
    if (datetime instanceof LocalDate)
        return (T) zdt.toLocalDate();
    else if (datetime instanceof LocalTime)
        return (T) zdt.toLocalTime();
    else if (datetime instanceof LocalDateTime)
        return (T) zdt.toLocalDateTime();
    else if (datetime instanceof Instant)
        return (T) zdt.toInstant();
    else
        return datetime;
}