Java中的ZonedDateTime及其示例
java.time.ZonedDateTime类是Java 8中添加的新日期和时间API的一部分,该API表示ISO-8601日历系统中具有时区的日期时间,例如2019-11-02T09:27:07+ 05:30 [Asia / Calcutta]。
Java ZonedDateTime类存储所有日期和时间字段(精度为纳秒)和时区(带区域偏移)。此类是不可变的,因此是线程安全的。由于已将其标记为最终的,因此无法进行扩展。在本文中,我们将看到一些示例,这些示例演示了Java ZonedDateTime类的用法。
创建ZonedDateTime的实例
Java中的ZonedDateTime类没有任何公共构造函数来获取实例,我们将使用工厂方法来获取实例。
1.使用now()方法,我们可以从系统时钟的默认时区中获取当前日期时间。
ZonedDateTime zdt = ZonedDateTime.now(); System.out.println(zdt);//2019-11-02T09:27:07.083270100+05:30[Asia/Calcutta]
我们还可以传递ZoneId以从指定时区中的系统时钟获取当前日期时间。
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Europe/Paris")); System.out.println(zdt);//2019-11-02T05:05:31.583917800+01:00[Europe/Paris]
2.我们可以使用of()方法通过传递年,月,日,小时,分钟,秒,纳秒和时区来获取ZonedDateTime的实例。
ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago")); System.out.println(zdt);//2019-10-25T15:10:21.000000252-05:00[America/Chicago]
我们还可以传递LocalDate,LocalTime和ZoneId的实例以获取ZonedDateTime
of(LocalDate date, LocalTime time, ZoneId zone)
我们还可以传递LocalDateTime和ZoneId的实例以获取ZonedDateTime
of(LocalDateTime localDateTime, ZoneId zone)
格式化ZonedDateTime(转换为字符串)
要将ZonedDateTime转换为String,可以使用DateTimeFormatter类,该类指定转换模式。
public class FormatDate { public static void main(String[] args) { ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago")); // Z - Time Zone offset DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a Z"); System.out.println(zdt.format(dtf)); // z - time zone name dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss z"); System.out.println(zdt.format(dtf)); //V - time-zone ID dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss VV"); System.out.println(zdt.format(dtf)); } }
输出:
2019-10-25 03:10:21 PM -0500 10/25/2019 15:10:21 GMT-05:00 10/25/2019 - 15:10:21 America/Chicago
将字符串转换为ZonedDateTime
使用parse()方法可以将String转换为ZonedDateTime。
public class FormatDate { public static void main(String[] args) { String dateWithZone = "10/25/2019 15:10:21 GMT-05:00"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss z"); ZonedDateTime zdt = ZonedDateTime.parse(dateWithZone, dtf); System.out.println(zdt); } }
输出:
2019-10-25T15:10:21-05:00[GMT-05:00]
从ZonedDateTime获取日期,时间和区域值
ZonedDateTime类具有获取年,月,日,时,分,秒值以及区域信息的方法。
public class FormatDate { public static void main(String[] args) { ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago")); System.out.println("Year- " + zdt.getYear()); System.out.println("Month- " + zdt.getMonthValue()); System.out.println("Day- " + zdt.getDayOfMonth()); System.out.println("Hour- " + zdt.getHour()); System.out.println("Minute- " + zdt.getMinute()); System.out.println("Second- " + zdt.getSecond()); System.out.println("NanoSecond- " + zdt.getNano()); System.out.println("Zone- " + zdt.getZone()); } }
输出:
Year- 2019 Month- 10 Day- 25 Hour- 15 Minute- 10 Second- 21 NanoSecond- 252 Zone- America/Chicago
在ZonedDateTime中添加或者减去年,月,日
有一些方法可以从Java ZonedDateTime中添加或者减去天,月和年。
public class FormatDate { public static void main(String[] args) { ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago")); System.out.println("Created Zoned Date-Time: " + zdt); System.out.println("Year after subtraction- " + zdt.minusYears(2)); System.out.println("Month after subtraction- " + zdt.minusMonths(4)); System.out.println("Day after subtraction- " + zdt.minusDays(35)); System.out.println("Year after addition- " + zdt.plusYears(2)); System.out.println("Month after addition- " + zdt.plusMonths(4)); System.out.println("Day after addition- " + zdt.plusDays(35)); } }
输出:
Created Zoned Date-Time: 2019-10-25T15:10:21.000000252-05:00[America/Chicago] Year after subtraction- 2017-10-25T15:10:21.000000252-05:00[America/Chicago] Month after subtraction- 2019-06-25T15:10:21.000000252-05:00[America/Chicago] Day after subtraction- 2019-09-20T15:10:21.000000252-05:00[America/Chicago] Year after addition- 2021-10-25T15:10:21.000000252-05:00[America/Chicago] Month after addition- 2020-02-25T15:10:21.000000252-06:00[America/Chicago] Day after addition- 2019-11-29T15:10:21.000000252-06:00[America/Chicago]
在ZonedDateTime中增加或者减去小时,分钟,秒
有一些方法可以从ZonedDateTime中添加或者减去小时,分钟,秒,纳秒。
public class FormatDate { public static void main(String[] args) { ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago")); System.out.println("Created Zoned Date-Time: " + zdt); System.out.println("Hour after subtraction- " + zdt.minusHours(2)); System.out.println("Minute after subtraction- " + zdt.minusMinutes(25)); System.out.println("Second after subtraction- " + zdt.minusSeconds(35)); System.out.println("Hour after addition- " + zdt.plusHours(2)); System.out.println("Minute after addition- " + zdt.plusMinutes(4)); System.out.println("Second after addition- " + zdt.plusSeconds(35)); System.out.println("NanoSecond after addition- " + zdt.plusNanos(250)); } }
输出:
Created Zoned Date-Time: 2019-10-25T15:10:21.000000252-05:00[America/Chicago] Hour after subtraction- 2019-10-25T13:10:21.000000252-05:00[America/Chicago] Minute after subtraction- 2019-10-25T14:45:21.000000252-05:00[America/Chicago] Second after subtraction- 2019-10-25T15:09:46.000000252-05:00[America/Chicago] Hour after addition- 2019-10-25T17:10:21.000000252-05:00[America/Chicago] Minute after addition- 2019-10-25T15:14:21.000000252-05:00[America/Chicago] Second after addition- 2019-10-25T15:10:56.000000252-05:00[America/Chicago] NanoSecond after addition- 2019-10-25T15:10:21.000000502-05:00[America/Chicago]
从ZonedDateTime获取LocalDateTime,LocalDate,LocalTime
public class FormatDate { public static void main(String[] args) { ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago")); System.out.println("Created Zoned Date-Time: " + zdt); LocalDateTime ldt = zdt.toLocalDateTime(); System.out.println("Extracted LocalDateTime: " + ldt); LocalDate ld = zdt.toLocalDate(); System.out.println("Extracted LocalDate: " + ld); LocalTime lt = zdt.toLocalTime(); System.out.println("Extracted LocalTime: " + lt); } }
输出:
Created Zoned Date-Time: 2019-10-25T15:10:21.000000252-05:00[America/Chicago] Extracted LocalDateTime: 2019-10-25T15:10:21.000000252 Extracted LocalDate: 2019-10-25 Extracted LocalTime: 15:10:21.000000252
在Java中比较ZonedDateTimes
为了比较两个ZonedDateTime实例,有以下方法:
compareTo(ChronoLocalDateTime <?other)-比较此日期时间与另一个日期时间。如果小于传递的LocalDateTime实例,则返回负值;如果大于,则返回正值。
isAfter(ChronoLocalDateTime <?other)-检查此日期时间是否在指定的日期时间之后。
isBefore(ChronoLocalDateTime <?other)-检查此日期时间是否早于指定的日期时间。
isEqual(ChronoLocalDateTime <?other)-检查此日期时间是否等于指定的日期时间。
public class FormatDate { public static void main(String[] args) { ZonedDateTime zdt1 = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago")); ZonedDateTime zdt2 = ZonedDateTime.of(2018, 8, 5, 4, 15, 21, 252, ZoneId.of("America/Chicago")); System.out.println("Created Zoned Date-Time1: " + zdt1); System.out.println("Created Zoned Date-Time2: " + zdt2); System.out.println(zdt1.compareTo(zdt2)); System.out.println(zdt2.compareTo(zdt1)); System.out.println(zdt1.isAfter(zdt2)); System.out.println(zdt1.isBefore(zdt2)); System.out.println(zdt1.isEqual(zdt2)); } }
输出:
Created Zoned Date-Time1: 2019-10-25T15:10:21.000000252-05:00[America/Chicago] Created Zoned Date-Time2: 2018-08-05T04:15:21.000000252-05:00[America/Chicago] 1 -1 true false false