【问题标题】:java zoneddatetime toEpochSecond without converting to local timejava zoneddatetime toEpochSecond 不转换为本地时间
【发布时间】:2020-09-28 19:10:52
【问题描述】:

我有一个在 EST 时间没有任何夏令时的数据集。 从字符串中读取每个日期时间,并使用

创建一个 zonedDatetime
ZonedDateTime java.time.ZonedDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)

带有 ZoneId.of("America/New_York");

我需要将这些转换为纪元秒,但内置的 toEpochSecond 方法会转换为我的本地时间,即 BST 夏令时。因此,根据一年中的时间,时间戳会缩短四到五个小时。有没有办法获得一个不考虑任何本地时间的 unix 时间戳,以便时间戳与原始字符串中的日期时间匹配?

【问题讨论】:

  • 是什么让您认为toEpochSecond 转换为 BST?它转换为无时区值。无论您使用什么来检查toEpochSecond 的值,都可能会插入您的本地时区。
  • from javadoc>将此日期时间转换为从 1970-01-01T00:00:00Z 开始的秒数。这使用本地日期时间和偏移量来计算纪元秒值,即从 1970-01-01T00:00:00Z 开始经过的秒数。纪元之后时间线上的瞬间是正的,更早的是负的。这是toEpochSecond的实际代码> default long toEpochSecond() { long epochDay = toLocalDate().toEpochDay(); long secs = epochDay * 86400 + toLocalTime().toSecondOfDay();秒 -= getOffset().getTotalSeconds();返回秒; }
  • 是的,而且?它使用来自ZonedDateTime 的区域“America/New_York”,这是完全正确的行为。

标签: java zoneddatetime


【解决方案1】:

将 ZonedDateTime 转换为 Unix 纪元时间戳

先转换为 java.time.Instant,然后将区域偏移设置为 UTC,然后再转换为纪元秒,见下文:

zonedDateTime.toInstant().atZone(ZoneOffset.UTC).toEpochSecond();

注意:变量 zonedDateTime 的类型为 java.time.ZonedDateTime,在将其转换为“Unix 纪元时间戳”之前可以处于任何时区(以秒为单位)。

【讨论】:

  • 谢谢,这就是我要找的东西
  • 太棒了!如果您想了解有关 java.time 的更多信息,请参阅:stackoverflow.com/questions/32437550/…。 940+票的答案。考虑到 java.time,这篇文章帮助我度过了难关。
【解决方案2】:

我需要将这些转换为纪元秒,但内置 toEpochSecond 方法转换为我的本地时间,即 BST 与天 省光。结果时间戳是四到五个小时 取决于一年中的时间。

你肯定做错了什么。检查以下代码的输出,你会发现并没有什么不同。

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Formatter ignoring nanoseconds
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                                        .appendPattern("uuuu-MM-dd'T'HH:mm:ssXXX")
                                        .optionalStart()
                                        .appendLiteral('[')
                                        .parseCaseSensitive()
                                        .appendZoneRegionId()
                                        .appendLiteral(']')
                                        .toFormatter(Locale.ENGLISH);

        // The given time-zone
        ZoneId zone = ZoneId.of("America/New_York");

        ZonedDateTime zdtNow = ZonedDateTime.now(zone);
        System.out.println(zdtNow.format(formatter));

        // Epoch seconds from ZonedDateTime
        long epochSecond = zdtNow.toEpochSecond();
        System.out.println(epochSecond);

        // ZonedDateTime from epoch seconds
        ZonedDateTime zdtFromEpochSeconds = Instant.ofEpochSecond(epochSecond).atZone(zone);
        System.out.println(zdtFromEpochSeconds.format(formatter));
    }
}

输出:

2020-09-28T17:31:23-04:00[America/New_York]
1601328683
2020-09-28T17:31:23-04:00[America/New_York]

【讨论】:

    猜你喜欢
    • 2017-05-31
    • 2018-08-12
    • 2019-08-05
    • 2018-12-24
    • 2015-11-07
    • 2023-03-26
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    相关资源
    最近更新 更多