在Epochmillis中获得不同时区的开始和末日 - Java

发布于 2025-02-11 13:02:49 字数 902 浏览 1 评论 0 原文

我正在尝试获得PST时区一天的开始时间(00:00:00)和一天的结束时间(23:59:59)。我尝试了以下代码,但是由于某种原因,我只能在UTC中获得开始和结束时间。我尝试更改时区以包括“ America/LOS_ANGELES”,但是输出时间戳始终显示GMT/UTC的开始和结束时间。

我的代码:

val time_zone = ZoneId.of("America/Los_Angeles")
val today_date = LocalDate.now(time_zone).plusDays(0)
val start_time = today_date + " " + "00:00:00"
val end_time = today_date + " " + "23:59:59"

val date_format = new SimpleDateFormat("yyyy-MM-dd");
val start_millis = date_format.parse(start_time).getTime();
val end_millis = date_format.parse(end_time).getTime();

start_millis

输出:

res375: Long = 1656460799000

在时期转换器中, 1656460799000 给我以下方式:

我在这里缺少什么?我应该更新任何软件包等吗?

I am trying to get the start time (00:00:00) and the end time (23:59:59) of a day in the PST time zone. I have tried the following code, but for some reason, I am only getting the start and end times in UTC. I have tried changing the timezone to include "America/Los_angeles", but the output timestamp is always showing start and end times for GMT/UTC.

My code:

val time_zone = ZoneId.of("America/Los_Angeles")
val today_date = LocalDate.now(time_zone).plusDays(0)
val start_time = today_date + " " + "00:00:00"
val end_time = today_date + " " + "23:59:59"

val date_format = new SimpleDateFormat("yyyy-MM-dd");
val start_millis = date_format.parse(start_time).getTime();
val end_millis = date_format.parse(end_time).getTime();

start_millis

Output:

res375: Long = 1656460799000

In the epoch converter, 1656460799000 gives me this:
The start time is 00:00 in UTC, not in America/Los_Angeles time

Anything I am missing here? Should I update any package, etc.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2025-02-18 13:02:49

java.time

现代方法仅使用 java.time 类。

无需使用 SimpleDateFormat date 日历和其他可怕的旧日期时间类。如果需要,您可以通过添加到旧类中的新转换方法来回并通过新的转换。

一天开始

我正在尝试获得开始时间(00:00:00)

不要假设这一天从00:00开始。某些区域的某些日期从另一个时间开始,例如01:00。令 java.time 使用 localdate#atstartofday 确定一天中的第一时刻。

一天结束

一天的结束时间(23:59:59)

您将缺少整天的最后一秒钟。

日期时间工作通常是通过半开的方法完成的。在半开局中,开始是包容性,而结尾为 exclusive 。因此,一天从一天中的第一刻开始,跑到但不包括第二天的第一时刻。半开的方法整齐地包含了全天的全部时间。

时区

PST时区。

没有名为 pst 的时区。流行媒体使用这样的2-4个字母伪区,以表明有关时区的提示。但是这些伪区是不是标准化的,甚至不是唯一的!仅用于向人类的局部演示,切勿用于数据存储或数据交换。

实时区域 code> code> code> code> code> code> contine> contine> conton/region/region/region

也许是“ PST”,您的意思是“太平洋标准时间”,通常表示 America/Tijuana ,或 America/LOS_ANGELES America/Vancouver 或其他的。

或者,也许是“ PST”,您的意思是“菲律宾标准时间”,涵盖了 ASIA/MANILA 时区。

示例代码

捕获当前时刻,如在时区所见。

ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

提取日期。

LocalDate today = zdt.toLocalDate() ;

确定一天的第一刻。

ZonedDateTime zdtStartOfDay = today.atStartOfDay( z ) ;

并确定第二天的第一时刻。

ZonedDateTime zdtStartOfFollowingDay = today.plusDays( 1 ).atStartOfDay( z ) ;

您可能需要看到时间长度。并非所有的日子都是24小时。

Duration d = Duration.between( zdtStartOfDay , zdtStartOfFollowingDay ) ;

通过提取 instant 对象。该课程代表了UTC中看到的时刻。

Instant start = zdtStartOfDay.toInstant() ;
Instant end = zdtStartOfFollowingDay.toInstant() ;

对于每一个,自1970年第一瞬间的时代引用自1970-01-01t00:00z所示。

long startMilli = start.toEpochMilli() ;
long endMilli = end.toEpochMilli() ;

但是,我强烈建议不要跟踪时间为毫秒。这种方法令人困惑,因为至少a 长无法由人类读者解释,因此错误可能会引起注意。

相反,数据存储和数据交换通常应使用标准 iso 8601 格式。 java.Time 类在解析/生成文本时默认使用这些标准格式。

String startText = start.toString() ;
String endText = end.toString() ;

threeten-extra

您可能需要添加 您的项目库。这使您可以访问 Interval 类,将时间跨度表示为一对即时对象。

Interval allDayLongToday = org.threeten.extra.Interval.of( start , end ) ;

该课程提供了几种有用的方法。这些包含包含,封闭, abuts union 互动等。

Instant invoiceRecorded = … some `Instant` ;
boolean invoiceRecordedToday = allDayLongToday.contains( invoiceRecorded ) ;

java.time

The modern approach uses the java.time classes only.

No need to ever use SimpleDateFormat, Date, Calendar, and the other terrible legacy date-time classes. If need be, you can convert to and fro via new conversion methods added to the old classes.

Start of day

I am trying to get the start time (00:00:00)

Do not assume the day starts at 00:00. Some dates in some zones start at another time such as 01:00. Let java.time determine the first moment of the day using LocalDate#atStartOfDay.

End of day

the end time (23:59:59) of a day

You would be missing an entire last second of the day with that approach.

Date-time work is commonly done with the Half-Open approach. In Half-Open, the beginning is inclusive while the ending is exclusive. So a day starts with the first moment of the day, and runs up to, but does not include, the first moment of the following day. Half-Open approach neatly contains that full last second of the day.

Time zones

PST time zone.

There is no such thing as a time zone named PST. Such 2-4 letter pseudo-zones are used by the popular media to indicate a hint about the time zone. But these pseudo-zones are not standardized, and are not even unique! Use only for localized presentation to humans, never for data storage or data exchange.

Real time zones are named with Continent/Region.

Perhaps by “PST” you meant “Pacific Standard Time”, which often indicates America/Tijuana, or America/Los_Angeles or America/Vancouver or others.

Or perhaps by “PST” you meant “Philippines Standard Time” covering the Asia/Manila time zone.

Example code

Capture the current moment as seen in a time zone.

ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Extract the date.

LocalDate today = zdt.toLocalDate() ;

Determine the first moment of the day.

ZonedDateTime zdtStartOfDay = today.atStartOfDay( z ) ;

And determine the first moment of the following day.

ZonedDateTime zdtStartOfFollowingDay = today.plusDays( 1 ).atStartOfDay( z ) ;

You may want to see the length of time. Not all days are 24 hours.

Duration d = Duration.between( zdtStartOfDay , zdtStartOfFollowingDay ) ;

Adjust both moments to UTC by extracting an Instant object. That class represents a moment as seen in UTC.

Instant start = zdtStartOfDay.toInstant() ;
Instant end = zdtStartOfFollowingDay.toInstant() ;

For each, get the count of milliseconds since the epoch reference of first moment of 1970 as seen in UTC, 1970-01-01T00:00Z.

long startMilli = start.toEpochMilli() ;
long endMilli = end.toEpochMilli() ;

However, I strongly recommend against tracking time as a count of milliseconds. This approach is confusing, as at least a couple dozen epoch reference points are commonly used. And a long cannot be interpreted by a human reader, so mistakes may go unnoticed.

Instead, data storage and data exchange should generally be done as text using the standard ISO 8601 formats. The java.time classes use these standard formats by default when parsing/generating text.

String startText = start.toString() ;
String endText = end.toString() ;

ThreeTen-Extra

You may want to add the ThreeTen-Extra library to your project. This gives you access to the Interval class, to represent a span of time as a pair of Instant objects.

Interval allDayLongToday = org.threeten.extra.Interval.of( start , end ) ;

This class provides several helpful methods. These include contains, encloses, abuts, union, intersection, and more.

Instant invoiceRecorded = … some `Instant` ;
boolean invoiceRecordedToday = allDayLongToday.contains( invoiceRecorded ) ;
疏忽 2025-02-18 13:02:49

只需将此部分添加到您的代码中:

date_format.setTimeZone(TimeZone.getTimeZone("PST"));

然后它将按照您的需求工作:)

Just add this section to your code:

date_format.setTimeZone(TimeZone.getTimeZone("PST"));

Then it will work as you want :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文