ZonedDateTime类
我们之前在文章《听说你还在用Date类表示时间?》中提到过LocalDateTime无法与时间戳进行转换,因为LocalDateTime没有时区,无法确定某一时刻。其实LocalDateTime和ZonedDateTime很相似,区别只是LocalDateTime总是表示本地日期和时间,而ZonedDateTime要表示一个带时区的日期和时间。
我们可以简单地把ZonedDateTime理解成LocalDateTime加ZoneId。ZoneId是java.time引入的新的时区类,注意和旧的java.util.TimeZone区别。
ZonedDateTime类的常用方法如下:
//从默认时区中的系统时钟中获取当前日期时间
static ZonedDateTime now()
//从指定的时钟中获取当前日期时间
static ZonedDateTime now(Clock clock)
//从指定时区中的系统时钟中获得当前日期时间
static ZonedDateTime now(ZoneId zone)
//获得 ZonedDateTime实例从年,月,日,小时,分钟,秒,纳秒和时区
static ZonedDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)
//获得 ZonedDateTime实例从本地日期和时间
static ZonedDateTime of(LocalDate date, LocalTime time, ZoneId zone)
//获得 ZonedDateTime实例从本地日期时间
static ZonedDateTime of(LocalDateTime localDateTime, ZoneId zone)
//获得 ZonedDateTime实例从一个文本字符串,如 2007-12-03T10:15:30+01:00[Europe/Paris]
static ZonedDateTime parse(CharSequence text)
//获得 ZonedDateTime实例从使用特定格式的文本字符串
static ZonedDateTime parse(CharSequence text, DateTimeFormatter formatter)
//返回此日期时间的副本,以不同的时区,保留即时
ZonedDateTime withZoneSameInstant(ZoneId zone)
补充一点,LocalDateTime类也提供了一个方法来获取ZonedDateTime对象
//结合时间与时区来创建一个 ZonedDateTime
ZonedDateTime atZone(ZoneId zone)
然后我们再来举几个例子。
范例:创建ZonedDateTime
package edu.blog.test07;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateTimeTestDemo01 {
public static void main(String[] args) {
//static ZonedDateTime now()方法
ZonedDateTime zdt01 = ZonedDateTime.now(); // 默认时区
ZonedDateTime zdt02 = ZonedDateTime.now(ZoneId.of("America/New_York")); // 用指定时区获取当前时间
System.out.println(zdt01);
System.out.println(zdt02);
System.out.println("===========================");
//static ZonedDateTime of()方法
LocalDateTime ldt01 = LocalDateTime.of(2000, 10, 30, 20, 00, 00);
ZonedDateTime zdt03 = ZonedDateTime.of(ldt01, ZoneId.of("America/New_York"));
System.out.println(ldt01);
System.out.println(zdt03);
System.out.println("===========================");
//LocalDateTime.atZone()方法
LocalDateTime ldt02 = LocalDateTime.of(2001, 7, 27, 22, 22, 22);
ZonedDateTime zdt04 = ldt02.atZone(ZoneId.systemDefault());
ZonedDateTime zdt05 = ldt02.atZone(ZoneId.of("America/New_York"));
System.out.println(zdt04);
System.out.println(zdt05);
}
}
/*
执行结果:
2021-05-02T20:30:54.777+08:00[Asia/Shanghai]
2021-05-02T08:30:54.777-04:00[America/New_York]
===========================
2000-10-30T20:00
2000-10-30T20:00-05:00[America/New_York]
===========================
2001-07-27T22:22:22+08:00[Asia/Shanghai]
2001-07-27T22:22:22-04:00[America/New_York]
*/
加下来,我们来说一说时区转换。要转换时区,首先我们需要有一个ZonedDateTime对象,然后,通过对象调用withZoneSameInstant()方法将关联时区转换到另一个时区,转换后日期和时间都会相应调整。
范例:时区转换
package edu.blog.test07;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateTimeTestDemo02 {
public static void main(String[] args) {
LocalDateTime ldt01 = LocalDateTime.of(2001, 9, 15, 20, 00, 00);
ZonedDateTime zdt01 = ZonedDateTime.of(ldt01, ZoneId.of("Asia/Shanghai"));
ZonedDateTime zdt02 = zdt01.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println(zdt01);
System.out.println(zdt02);
System.out.println("===============================");
LocalDateTime ldt02 = LocalDateTime.of(2001, 4, 15, 20, 00, 00);
ZonedDateTime zdt03 = ZonedDateTime.of(ldt01, ZoneId.of("Asia/Shanghai"));
ZonedDateTime zdt04 = zdt01.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println(zdt03);
System.out.println(zdt04);
}
}
/*
执行结果:
2001-09-15T20:00+08:00[Asia/Shanghai]
2001-09-15T08:00-04:00[America/New_York]
===============================
2001-09-15T20:00+08:00[Asia/Shanghai]
2001-09-15T08:00-04:00[America/New_York]
*/
OK,那先就说到这吧。
注:此文章为个人学习笔记,如有错误,敬请指正。
版权声明:本文为Window_mouse原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。