如何获取日期的utc日期?

92dk7w1h  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(299)

这个问题在这里已经有答案了

如何将日期转换为utc(2个答案)
56分钟前关门了。
下面是我的应用程序中的java pojo:

public class Appointment {

    private Date appointmentStartDatetime;

    private String timezone;

    //...
}

这两个属性的示例值为:

2021-10-06 05:30:00
"America/New_York"
``` `appointmentStartDatetime` 表示在其声明的时区中的约会开始时间-即,在纽约,约会从5:30开始。
我想把它转换成utc日期。
为了做到这一点,下面的说明是否正确?

public static Date dateToUTC(Date date, String timezoneId){

    Calendar calendar = Calendar.getInstance();
    TimeZone timeZone = TimeZone.getTimeZone(timezoneId);
    calendar.setTimeZone(timeZone);

    return new Date(date.getTime() - calendar.getTimeZone().getOffset(date.getTime()));
}
方法调用如下:

Date utcDate = dateToUTC(appointment.getEventStartDatetime(),appointment.getTimezone())

hpxqektj

hpxqektj1#

这个 java.util.Date 对象不是像现代日期时间类型那样的实时日期时间对象;相反,它表示自称为“epoch”的标准基准时间以来的毫秒数,即 January 1, 1970, 00:00:00 GMT (或utc)。打印对象时 java.util.Date ,其 toString 方法返回jvm时区中的日期时间,该值是从该毫秒值计算出来的。如果需要在不同的时区打印日期时间,则需要将时区设置为 SimpleDateFormat 并从中获取格式化字符串。
注意,api的日期时间 java.util 以及它们的格式化api, SimpleDateFormat 过时且容易出错。建议完全停止使用它们,并切换到现代日期时间api。
出于任何原因,如果您必须坚持使用Java6或Java7,您可以使用threeten backport,它将大部分java.time功能向后移植到Java6和Java7。
如果您正在为一个android项目工作,并且您的android api级别仍然不符合java-8,请检查通过desugaring提供的java8+api以及如何在android项目中使用threetenabp。
使用现代日期时间api:

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

class Appointment {
    private LocalDateTime appointmentStartDatetime;
    private String timezone;

    public Appointment(LocalDateTime appointmentStartDatetime, String timezone) {
        this.appointmentStartDatetime = appointmentStartDatetime;
        this.timezone = timezone;
    }

    public LocalDateTime getAppointmentInLocalDateTime() {
        return appointmentStartDatetime;
    }

    public Instant getAppointmentInUTC() {
        return appointmentStartDatetime.toInstant(ZoneOffset.UTC);
    }

    public ZonedDateTime getAppointmentInZonedDateTime() {
        return appointmentStartDatetime.atZone(ZoneId.of(timezone));
    }

    public ZonedDateTime getAppointmentInZdtAnotherTimeZone(String timezone) {
        return getAppointmentInZonedDateTime().withZoneSameInstant(ZoneId.of(timezone));
    }
    // ...
}

public class Main {
    public static void main(String[] args) {
        Appointment appointment = new Appointment(
                LocalDateTime.of(LocalDate.of(2020, Month.FEBRUARY, 15), LocalTime.of(10, 30)), "America/New_York");

        System.out.println(appointment.getAppointmentInLocalDateTime());
        System.out.println(appointment.getAppointmentInUTC());
        System.out.println(appointment.getAppointmentInZonedDateTime());
        System.out.println(appointment.getAppointmentInZdtAnotherTimeZone("Asia/Calcutta"));
    }
}

输出:

2020-02-15T10:30
2020-02-15T10:30:00Z
2020-02-15T10:30-05:00[America/New_York]
2020-02-15T21:00+05:30[Asia/Calcutta]

Instant 表示时间线上的瞬时点。
ZonedDateTime 保持相当于三个独立对象的状态 LocalDateTime ,一个 ZoneId 解决了这个问题 ZoneOffset . 函数, ZonedDateTime#withZoneSameInstant 返回具有不同时区的此日期时间的副本,保留即时消息。
下面是java se 8日期时间类型的概述:

从trail:date-time了解有关现代日期时间api的更多信息。

jdzmm42g

jdzmm42g2#

java.time文件

避免混淆使用 ZonedDateTime 从java.time,现代java日期和时间api,为您的日期和时区时间。

public class Appointment {

    private ZonedDateTime appointmentStartDateTime;

    // Constructor, etc.

    public Instant getUtcDateTime() {
        return appointmentStartDateTime.toInstant();
    }

}

试一试:

Appointment myAppointment = new Appointment(
                ZonedDateTime.of(2021, 1, 10, 5, 30, 0, 0,
                        ZoneId.of("America/New_York")));

        System.out.println(myAppointment.getUtcDateTime());

输出为:
2021-01-10t10:30:00z
ZonedDateTime 是时区中的日期和时间。所以你只需要这一个字段,而不是两个字段, appointmentStartDatetime 以及 timezone . 一 Instant 是一个独立于时区的时间点。它以utc格式打印,用尾随字符表示 Z 上面的输出。以及从 ZonedDateTimeInstant 是一个简单的方法调用。

链接

oracle教程:date time解释如何使用java.time。

相关问题