如何从时间戳中获取小时、日和周的开始时间?

The*_*ger 0 java spring timestamp spring-boot

我有以下时间戳:

Timestamp time = new Timestamp(1652039000000L);
Run Code Online (Sandbox Code Playgroud)

这次是"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"

deH*_*aar 5

你可以用java.time这个,你基本上必须

\n
    \n
  • Instant从毫秒创建一个(不是a Timestamp
  • \n
  • 使用ZonedDateTime所需的时区和Instant
  • \n
  • 使用类提供的方法java.time来截断或调整时间和
  • \n
  • 最后,将Instant从一个转换ZoneId为另一个(这里是从"Europe/Paris""UTC"
  • \n
\n

这是一个例子:

\n
public static void main(String[] args) {\n    // first of all, use an Instant, not a Timestamp for conversion\n    Instant time = Instant.ofEpochMilli(1652039000000L);\n    // define the zone for your time\n    ZoneId paris = ZoneId.of("Europe/Paris");\n    // then create a ZonedDateTime of it at the desired zone\n    ZonedDateTime parisTime = ZonedDateTime.ofInstant(time, paris);\n    // (1) truncate the time to hours\n    ZonedDateTime parisTimeTilHour = parisTime.truncatedTo(ChronoUnit.HOURS);\n    // (2) truncate the time to days\n    ZonedDateTime parisTimeDateOnly = parisTime.truncatedTo(ChronoUnit.DAYS);\n    // (3) get the first day of week and truncate that to days\n    ZonedDateTime parisTimeStartOfWeek = parisTime.with(WeekFields.ISO.getFirstDayOfWeek())\n                                       .truncatedTo(ChronoUnit.DAYS);\n    // define a formatter to be used for output\n    DateTimeFormatter isoLDT = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.S");\n    // print the results:\n    System.out.println("Paris: " + parisTime.format(isoLDT));\n    System.out.println(" --> " + parisTimeTilHour.format(isoLDT));\n    System.out.println(" --> " + parisTimeDateOnly.format(isoLDT));\n    System.out.println(" --> " + parisTimeStartOfWeek.format(isoLDT));\n    \n    // Shift the zone to UTC, create UTC as ZoneId first\xe2\x80\xa6\n    ZoneId utc = ZoneId.of("UTC");\n    ZonedDateTime utcTime = parisTime.withZoneSameInstant(utc);\n    ZonedDateTime utcTimeTilHour = parisTime.withZoneSameInstant(utc);\n    ZonedDateTime utcDateOnly = parisTimeDateOnly.withZoneSameInstant(utc);\n    ZonedDateTime utcWeekStart = parisTimeStartOfWeek.withZoneSameInstant(utc);\n    // print\xe2\x80\xa6\n    System.out.println("UTC  : " + utcTime.format(isoLDT));\n    System.out.println(" --> " + utcTimeTilHour.format(isoLDT));\n    System.out.println(" --> " + utcDateOnly.format(isoLDT));\n    System.out.println(" --> " + utcWeekStart.format(isoLDT));\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这个例子将输出

\n
Paris: 2022-05-08 21:43:20.0\n --> 2022-05-08 21:00:00.0\n --> 2022-05-08 00:00:00.0\n --> 2022-05-02 00:00:00.0\nUTC  : 2022-05-08 19:43:20.0\n --> 2022-05-08 19:43:20.0\n --> 2022-05-07 22:00:00.0\n --> 2022-05-01 22:00:00.0\n
Run Code Online (Sandbox Code Playgroud)\n

如果你真的有一个Timestamp唯一的,你必须提取毫秒才能创建一个Instant\xe2\x80\xa6 ,这不再需要了,现在有一种方法可以实现遗留兼容性,那就是Timestamp.toInstant().

\n