在Epochmillis中获得不同时区的开始和末日 - Java
我正在尝试获得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
给我以下方式:
我在这里缺少什么?我应该更新任何软件包等吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
java.time
现代方法仅使用 java.time 类。
无需使用
SimpleDateFormat
,date
,日历
和其他可怕的旧日期时间类。如果需要,您可以通过添加到旧类中的新转换方法来回并通过新的转换。一天开始
不要假设这一天从00:00开始。某些区域的某些日期从另一个时间开始,例如01:00。令 java.time 使用
localdate#atstartofday
确定一天中的第一时刻。一天结束
您将缺少整天的最后一秒钟。
日期时间工作通常是通过半开的方法完成的。在半开局中,开始是包容性,而结尾为 exclusive 。因此,一天从一天中的第一刻开始,跑到但不包括第二天的第一时刻。半开的方法整齐地包含了全天的全部时间。
时区
没有名为
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
时区。示例代码
捕获当前时刻,如在时区所见。
提取日期。
确定一天的第一刻。
并确定第二天的第一时刻。
您可能需要看到时间长度。并非所有的日子都是24小时。
通过提取
instant
对象。该课程代表了UTC中看到的时刻。对于每一个,自1970年第一瞬间的时代引用自1970-01-01t00:00z所示。
但是,我强烈建议不要跟踪时间为毫秒。这种方法令人困惑,因为至少a 长无法由人类读者解释,因此错误可能会引起注意。
相反,数据存储和数据交换通常应使用标准 iso 8601 格式。 java.Time 类在解析/生成文本时默认使用这些标准格式。
threeten-extra
您可能需要添加 您的项目库。这使您可以访问 Interval 类,将时间跨度表示为一对
即时
对象。该课程提供了几种有用的方法。这些包含
包含
,封闭,abuts
,union
,互动
等。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
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
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
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
, orAmerica/Los_Angeles
orAmerica/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.
Extract the date.
Determine the first moment of the day.
And determine the first moment of the following day.
You may want to see the length of time. Not all days are 24 hours.
Adjust both moments to UTC by extracting an
Instant
object. That class represents a moment as seen in UTC.For each, get the count of milliseconds since the epoch reference of first moment of 1970 as seen in UTC, 1970-01-01T00:00Z.
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.
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 ofInstant
objects.This class provides several helpful methods. These include
contains
,encloses
,abuts
,union
,intersection
, and more.只需将此部分添加到您的代码中:
然后它将按照您的需求工作:)
Just add this section to your code:
Then it will work as you want :)