如何从时间戳中获取小时、天和周的开始?
How do I get start of the hour, day and week from a timestamp?
我有以下时间戳:
Timestamp time = new Timestamp(1652039000000L);
这次是当地时间"2022-05-08 19:43:20.0"
。有没有办法把它变成当前的开始:
- 小时:
"2022-05-08 19:00:00.0"
- 天:
"2022-05-08 00:00:00.0"
- 周:
"2022-05-02 00:00:00.0"
然后从中减去 UTC 时间,这样巴黎(+2 小时)就会 return:
- 小时:
"2022-05-08 17:00:00.0"
- 天:
"2022-05-07 22:00:00.0"
- 周:
"2022-05-01 22:00:00.0"
使用 timeStamp.toLocalDateTime().toLocalDate();
将 TimeStamp 转换为 LocalDate,然后通过以下代码获取一周的开始时间:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2022, 5, 15);
date = date.with(nextNthDayOfWeek(0, DayOfWeek.MONDAY));
System.out.println("date = " + date);
}
public static TemporalAdjuster nextNthDayOfWeek(int n, DayOfWeek dayOfWeek) {
return temporal -> {
Temporal next = temporal.with(TemporalAdjusters.next(dayOfWeek));
return next.plus(n - 1, ChronoUnit.WEEKS);
};
}
}
你可以用 java.time
来做这个,你基本上必须
- 从毫秒创建一个
Instant
(不是一个Timestamp
)
- 使用所需时区和
Instant
创建 ZonedDateTime
- 使用
java.time
类提供的方法截断或调整时间
- 最后,将
Instant
从一个 ZoneId
转换为另一个(这里从 "Europe/Paris"
转换为 "UTC"
)
这是一个例子:
public static void main(String[] args) {
// first of all, use an Instant, not a Timestamp for conversion
Instant time = Instant.ofEpochMilli(1652039000000L);
// define the zone for your time
ZoneId paris = ZoneId.of("Europe/Paris");
// then create a ZonedDateTime of it at the desired zone
ZonedDateTime parisTime = ZonedDateTime.ofInstant(time, paris);
// (1) truncate the time to hours
ZonedDateTime parisTimeTilHour = parisTime.truncatedTo(ChronoUnit.HOURS);
// (2) truncate the time to days
ZonedDateTime parisTimeDateOnly = parisTime.truncatedTo(ChronoUnit.DAYS);
// (3) get the first day of week and truncate that to days
ZonedDateTime parisTimeStartOfWeek = parisTime.with(WeekFields.ISO.getFirstDayOfWeek())
.truncatedTo(ChronoUnit.DAYS);
// define a formatter to be used for output
DateTimeFormatter isoLDT = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.S");
// print the results:
System.out.println("Paris: " + parisTime.format(isoLDT));
System.out.println(" --> " + parisTimeTilHour.format(isoLDT));
System.out.println(" --> " + parisTimeDateOnly.format(isoLDT));
System.out.println(" --> " + parisTimeStartOfWeek.format(isoLDT));
// Shift the zone to UTC, create UTC as ZoneId first…
ZoneId utc = ZoneId.of("UTC");
ZonedDateTime utcTime = parisTime.withZoneSameInstant(utc);
ZonedDateTime utcTimeTilHour = parisTime.withZoneSameInstant(utc);
ZonedDateTime utcDateOnly = parisTimeDateOnly.withZoneSameInstant(utc);
ZonedDateTime utcWeekStart = parisTimeStartOfWeek.withZoneSameInstant(utc);
// print…
System.out.println("UTC : " + utcTime.format(isoLDT));
System.out.println(" --> " + utcTimeTilHour.format(isoLDT));
System.out.println(" --> " + utcDateOnly.format(isoLDT));
System.out.println(" --> " + utcWeekStart.format(isoLDT));
}
这个例子会输出
Paris: 2022-05-08 21:43:20.0
--> 2022-05-08 21:00:00.0
--> 2022-05-08 00:00:00.0
--> 2022-05-02 00:00:00.0
UTC : 2022-05-08 19:43:20.0
--> 2022-05-08 19:43:20.0
--> 2022-05-07 22:00:00.0
--> 2022-05-01 22:00:00.0
如果你真的只有一个 Timestamp
并且你必须提取 millis 以创建一个 Instant
... 不再需要了,现在有一种方法可以实现遗留兼容性,即 Timestamp.toInstant()
.
我有以下时间戳:
Timestamp time = new Timestamp(1652039000000L);
这次是当地时间"2022-05-08 19:43:20.0"
。有没有办法把它变成当前的开始:
- 小时:
"2022-05-08 19:00:00.0"
- 天:
"2022-05-08 00:00:00.0"
- 周:
"2022-05-02 00:00:00.0"
然后从中减去 UTC 时间,这样巴黎(+2 小时)就会 return:
- 小时:
"2022-05-08 17:00:00.0"
- 天:
"2022-05-07 22:00:00.0"
- 周:
"2022-05-01 22:00:00.0"
使用 timeStamp.toLocalDateTime().toLocalDate();
将 TimeStamp 转换为 LocalDate,然后通过以下代码获取一周的开始时间:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2022, 5, 15);
date = date.with(nextNthDayOfWeek(0, DayOfWeek.MONDAY));
System.out.println("date = " + date);
}
public static TemporalAdjuster nextNthDayOfWeek(int n, DayOfWeek dayOfWeek) {
return temporal -> {
Temporal next = temporal.with(TemporalAdjusters.next(dayOfWeek));
return next.plus(n - 1, ChronoUnit.WEEKS);
};
}
}
你可以用 java.time
来做这个,你基本上必须
- 从毫秒创建一个
Instant
(不是一个Timestamp
) - 使用所需时区和
Instant
创建 - 使用
java.time
类提供的方法截断或调整时间 - 最后,将
Instant
从一个ZoneId
转换为另一个(这里从"Europe/Paris"
转换为"UTC"
)
ZonedDateTime
这是一个例子:
public static void main(String[] args) {
// first of all, use an Instant, not a Timestamp for conversion
Instant time = Instant.ofEpochMilli(1652039000000L);
// define the zone for your time
ZoneId paris = ZoneId.of("Europe/Paris");
// then create a ZonedDateTime of it at the desired zone
ZonedDateTime parisTime = ZonedDateTime.ofInstant(time, paris);
// (1) truncate the time to hours
ZonedDateTime parisTimeTilHour = parisTime.truncatedTo(ChronoUnit.HOURS);
// (2) truncate the time to days
ZonedDateTime parisTimeDateOnly = parisTime.truncatedTo(ChronoUnit.DAYS);
// (3) get the first day of week and truncate that to days
ZonedDateTime parisTimeStartOfWeek = parisTime.with(WeekFields.ISO.getFirstDayOfWeek())
.truncatedTo(ChronoUnit.DAYS);
// define a formatter to be used for output
DateTimeFormatter isoLDT = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.S");
// print the results:
System.out.println("Paris: " + parisTime.format(isoLDT));
System.out.println(" --> " + parisTimeTilHour.format(isoLDT));
System.out.println(" --> " + parisTimeDateOnly.format(isoLDT));
System.out.println(" --> " + parisTimeStartOfWeek.format(isoLDT));
// Shift the zone to UTC, create UTC as ZoneId first…
ZoneId utc = ZoneId.of("UTC");
ZonedDateTime utcTime = parisTime.withZoneSameInstant(utc);
ZonedDateTime utcTimeTilHour = parisTime.withZoneSameInstant(utc);
ZonedDateTime utcDateOnly = parisTimeDateOnly.withZoneSameInstant(utc);
ZonedDateTime utcWeekStart = parisTimeStartOfWeek.withZoneSameInstant(utc);
// print…
System.out.println("UTC : " + utcTime.format(isoLDT));
System.out.println(" --> " + utcTimeTilHour.format(isoLDT));
System.out.println(" --> " + utcDateOnly.format(isoLDT));
System.out.println(" --> " + utcWeekStart.format(isoLDT));
}
这个例子会输出
Paris: 2022-05-08 21:43:20.0
--> 2022-05-08 21:00:00.0
--> 2022-05-08 00:00:00.0
--> 2022-05-02 00:00:00.0
UTC : 2022-05-08 19:43:20.0
--> 2022-05-08 19:43:20.0
--> 2022-05-07 22:00:00.0
--> 2022-05-01 22:00:00.0
如果你真的只有一个 Timestamp
并且你必须提取 millis 以创建一个 Instant
... 不再需要了,现在有一种方法可以实现遗留兼容性,即 Timestamp.toInstant()
.