我如何从时间戳开始时,每天和一周?
我有以下时间戳:
Timestamp time = new Timestamp(1652039000000L);
这次是“ 2022-05-08 19:43:20.0”
在本地时间。有什么方法可以将其转变为当前的开始:
- 小时:
“ 2022-05-08 19:00:00.0”
- day:
“ 2022-05-05-08 00:00: 00.0“
- 周:
” 2022-05-02 00:00:00.0“
,然后减去UTC时间,以便巴黎(+2小时)返回:
-
“ 2022-05-08 17:00:00.0” - 天:
“ 2022-05-07 22:00:00.0:00:00.0”
- 周:
22:00:00.0“
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为此使用
java.Time
,基本上必须a
timestamp <)创建
Instant
/code>)ZonedDateTime
使用所需时区和Instant
Java.Time
类提供的方法,以截断或调整时间和Instant
从一个zydiD
转换为另一个(此处,从“欧洲/巴黎”
从)以下示例:
此示例将输出
,如果您确实有一个
Timestamp
,则必须提取Millis才能创建Instant
…这不再是必需的,现在有一种传统兼容性的方法,即timestamp.toinstant()
。You could use
java.time
for this, you'd basically have toInstant
from the milliseconds (not aTimestamp
)ZonedDateTime
using a desired time zone and theInstant
java.time
classes in order to truncate or adjust the time andInstant
from oneZoneId
to another one (here from"Europe/Paris"
to"UTC"
)Here's an example:
This example will output
And if you really have a
Timestamp
only and you would have to extract the millis in order to create anInstant
… That's not necessary anymore, there is a method now for legacy compatibility, that isTimestamp.toInstant()
.使用
timestamp.tolocaldateTime()。tolocaldate();
用于将时间戳转换为localdate,然后通过以下代码获得一周的开始:Java 8:在一个月的特定日期之后查找nth dayofweek
Use
timeStamp.toLocalDateTime().toLocalDate();
for converting TimeStamp to LocalDate then get start of week by following code:Java 8: Find nth DayOfWeek after specific day of month