我如何从时间戳开始时,每天和一周?

发布于 2025-01-29 05:02:55 字数 566 浏览 4 评论 0 原文

我有以下时间戳:

Timestamp time = new Timestamp(1652039000000L);

这次是“ 2022-05-08 19:43:20.0” 在本地时间。有什么方法可以将其转变为当前的开始:

  1. 小时:“ 2022-05-08 19:00:00.0”
  2. day:“ 2022-05-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:00:00.0”
  3. 周: 22:00:00.0“

I have the following Timestamp:

Timestamp time = new Timestamp(1652039000000L);

This time is "2022-05-08 19:43:20.0" in local time. Are there any ways to turn this into the start of the current:

  1. Hour: "2022-05-08 19:00:00.0"
  2. Day: "2022-05-08 00:00:00.0"
  3. Week: "2022-05-02 00:00:00.0"

And then minus get the UTC time from it so that Paris (+2 hours) would return:

  1. Hour: "2022-05-08 17:00:00.0"
  2. Day: "2022-05-07 22:00:00.0"
  3. Week: "2022-05-01 22:00:00.0"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云 2025-02-05 05:02:55

您可以为此使用 java.Time ,基本上必须

  • 从毫秒( not a a timestamp <)创建 Instant /code>)
  • 创建 ZonedDateTime 使用所需时区和 Instant
  • 使用 Java.Time 类提供的方法,以截断或调整时间和
  • 最后,将 Instant 从一个 zydiD 转换为另一个(此处,从“欧洲/巴黎” 从)

以下示例:

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()

You could use java.time for this, you'd basically have to

  • create an Instant from the milliseconds (not a Timestamp)
  • create a ZonedDateTime using a desired time zone and the Instant
  • use methods provided by java.time classes in order to truncate or adjust the time and
  • lastly, convert the Instant from one ZoneId to another one (here from "Europe/Paris" to "UTC")

Here's an example:

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

This example will 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

And if you really have a Timestamp only and you would have to extract the millis in order to create an Instant… That's not necessary anymore, there is a method now for legacy compatibility, that is Timestamp.toInstant().

街角卖回忆 2025-02-05 05:02:55

使用 timestamp.tolocaldateTime()。tolocaldate(); 用于将时间戳转换为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:在一个月的特定日期之后查找nth dayofweek

Use timeStamp.toLocalDateTime().toLocalDate(); for converting TimeStamp to LocalDate then get start of week by following code:

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: Find nth DayOfWeek after specific day of month

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文