如何从时间戳中获取时间、日期和星期的开始



我有以下时间戳:

Timestamp time = new Timestamp(1652039000000L);

这个时间是当地时间的"2022-05-08 19:43:20.0"。有没有什么方法可以把它变成当前的开始:

  1. 小时:"2022-05-08 19:00:00.0"
  2. 日期:"2022-05-08 00:00:00.0"
  3. 周:"2022-05-02 00:00:00.0"

然后减去UTC时间,这样巴黎(+2小时)就会返回:

  1. 小时:"2022-05-08 17:00:00.0"
  2. 日期:"2022-05-07 22:00:00.0"
  3. 周:"2022-05-01 22:00:00.0"

你可以使用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.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 8:在一个月的特定日期之后查找周的第n天

最新更新