原文: https://howtodoinjava.com/java/date-time/zoneddatetime-class/
Java 8 中引入的java.time.ZonedDateTime类表示 ISO-8601 日历系统中具有时区信息的日期和时间。 此类存储所有日期和时间字段,精度为纳秒。
我们可以使用ZonedDateTime实例来代表全球分布的用户的时间。 例如,我们可以使用ZonedDateTime传达会议日期,参加者将根据他们的本地日期和时间在线连接。
ZonedDateTime的状态等于三个单独的对象:LocalDateTime,ZoneId和已解析的ZoneOffset。
1. ZonedDateTime类声明
public final class ZonedDateTimeextends Objectimplements Temporal, ChronoZonedDateTime<LocalDate>, Serializable{//class body}
2. 如何创建ZonedDateTime
通常,我们将在两种情况下创建ZonedDateTime实例,即获取具有区域信息的当前时间戳或创建代表特定时区的时间戳。
2.1 获取当前的ZonedDateTime
使用以下工厂方法获取当前时间戳。
ZonedDateTime now = ZonedDateTime.now();ZonedDateTime now = ZonedDateTime.now( ZoneId.of("GMT+05:30") ); //Time in IST
2.2 创建指定的ZonedDateTime
要创建带有特定日期,时间和区域信息的时间戳,请使用以下方法。
//1 - All date parts are inplaceZoneId zoneId = ZoneId.of("UTC+1");ZonedDateTime zonedDateTime1 =ZonedDateTime.of(2015, 11, 30, 23, 45, 59, 1234, zoneId);//=========//2 - Using existing local date and time valuesLocalDate localDate = LocalDate.of(2019, 03, 12);LocalTime localTime = LocalTime.of(12, 44);ZoneId zoneId = ZoneId.of("GMT+05:30");ZonedDateTime timeStamp = ZonedDateTime.of( localDate, localTime, zoneId );
3. 如何将字符串解析为ZonedDateTime
ZonedDateTime类具有两个重载的parse()方法,用于将字符串中的时间转换为本地时间实例。
parse(CharSequence text) //1parse(CharSequence text, DateTimeFormatter formatter) //2
- 如果字符串以
ISO_ZONED_DATE_TIME模式包含时间,即2019-03-28T10:15:30+01:00[Europe/Paris]则使用第一种方法。 这是默认模式。 - 对于任何其他日期时间模式,我们都需要使用第二种方法,在该方法中我们将日期时间作为字符串传递,并使用格式化器来表示该日期时间字符串的模式。
//1 - default patternString timeStamp = "2019-03-27T10:15:30";ZonedDateTime localTimeObj = ZonedDateTime.parse(time);//2 - specified patternDateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");String timeStamp1 = "2019-03-27 10:15:30 AM";ZonedDateTime localTimeObj1 = ZonedDateTime.parse(timeStamp1, formatter);
4. 如何将ZonedDateTime格式化为字符串
使用ZonedDateTime.format(DateTimeFormatter)方法将本地时间格式化为所需的字符串表示形式。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");ZonedDateTime now = ZonedDateTime.now();String dateTimeString = now.format(formatter); //2019-03-28 14:47:33 PM
5. 如何修改ZonedDateTime
ZonedDateTime提供以下修改方法。 所有方法都返回ZonedDateTime的新实例,因为现有实例始终是不可变的。
plusYears()plusMonths()plusDays()plusHours()plusMinutes()plusSeconds()plusNanos()minusYears()minusMonths()minusDays()minusHours()minusMinutes()minusSeconds()minusNanos()
ZonedDateTime now = ZonedDateTime.now();//3 hours laterZonedDateTime zonedDateTime1 = now.plusHours(3);//3 minutes earliarZonedDateTime zonedDateTime2 = now.minusMinutes(3);//Next year same timeZonedDateTime zonedDateTime2 = now.plusYears(1);//Last year same timeZonedDateTime zonedDateTime2 = now.minusYears(1);
6. 更多例子
在 Java 中将LocalDate转换为ZonedDateTime
在评论中向我发送有关 Java 8 ZonedDateTime类的问题。
学习愉快!
