繁体   English   中英

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

[英]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" 有什么办法可以把它变成当前的开始:

  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));
}

这个例子将 output

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并且你必须提取毫秒以创建一个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 个 DayOfWeek

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM