📜  Java8 ZonedDateTime类

📅  最后修改于: 2020-10-01 06:37:43             🧑  作者: Mango

Java ZonedDateTime类

Java ZonedDateTime类是带有时区的日期时间的不可变表示形式。它继承了Object类并实现了ChronoZonedDateTime接口。

ZonedDateTime类用于存储所有日期和时间字段,精度为纳秒,并且带有时区偏移量的时区用于处理模棱两可的本地日期时间。

Java ZonedDateTime类声明

我们来看一下java.time.ZonedDateTime类的声明。

public final class ZonedDateTime extends Object 
implements Temporal, ChronoZonedDateTime, Serializable

Java ZonedDateTime的方法

Method Description
String format(DateTimeFormatter formatter) It is used to format this date-time using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this date-time as an int.
ZoneId getZone() It is used to get the time-zone, such as ‘Asia/Kolkata’.
ZonedDateTime withZoneSameInstant(ZoneId zone) It is used to return a copy of this date-time with a different time-zone, retaining the instant.
static ZonedDateTimenow() It is used to obtain the current date-time from the system clock in the default time-zone.
static ZonedDateTimeof(LocalDate date, LocalTime time, ZoneId zone) It is used to obtain an instance of ZonedDateTime from a local date and time.
ZonedDateTime minus(long amountToSubtract, TemporalUnit unit) It is used to return a copy of this date-time with the specified amount subtracted.
ZonedDateTime plus(long amountToAdd, TemporalUnit unit) It is used to return a copy of this date-time with the specified amount added.

Java ZonedDateTime类示例

import java.time.ZonedDateTime;
public class ZonedDateTimeExample1{
  public static void main(String[] args) {
    ZonedDateTime zone = ZonedDateTime.parse("2016-10-05T08:20:10+05:30[Asia/Kolkata]");
    System.out.println(zone);
  }
}

输出:

2016-10-05T08:20:10+05:30[Asia/Kolkata]

Java ZonedDateTime类示例:of()和withZoneSameInstant()

import java.time.*;
public class ZonedDateTimeExample2{
  public static void main(String[] args) {
    LocalDateTime  ldt = LocalDateTime.of(2017, Month.JANUARY,  19,   15,   26);
    ZoneId  india = ZoneId.of("Asia/Kolkata"); 
    ZonedDateTime zone1  = ZonedDateTime.of(ldt, india); 
    System.out.println("In India Central Time Zone: " + zone1);
    ZoneId  tokyo = ZoneId.of("Asia/Tokyo"); 
    ZonedDateTime zone2   = zone1.withZoneSameInstant(tokyo); 
    System.out.println("In Tokyo Central Time Zone:"  + zone2);
  }
}

输出:

In India Central Time Zone: 2017-01-19T15:26+05:30[Asia/Kolkata]
In Tokyo Central Time Zone:2017-01-19T18:56+09:00[Asia/Tokyo]

Java ZonedDateTime类示例:getZone()

import java.time.ZonedDateTime;
public class ZonedDateTimeExample3{
  public static void main(String[] args) {
    ZonedDateTime zone =ZonedDateTime.now();
    System.out.println(zone.getZone());
  } 
}

输出:

Asia/Kolkata

Java ZonedDateTime类示例:minus()

import java.time.Period;
import java.time.ZonedDateTime;
public class ZonedDateTimeExample4 {
  public static void main(String[] args) {
    ZonedDateTime zone= ZonedDateTime.now();
    ZonedDateTime m = zone.minus(Period.ofDays(126));
    System.out.println(m);
  } 
}

输出:

2016-09-15T12:54:01.354+05:30[Asia/Kolkata]

Java ZonedDateTime类示例:plus()

import java.time.*;
public class ZonedDateTimeExample5{
  public static void main(String[] args) {
    ZonedDateTime zone= ZonedDateTime.now();
    ZonedDateTime p = zone.plus(Period.ofDays(126));
    System.out.println(p);
  } 
}

输出:

2017-05-25T12:56:12.417+05:30[Asia/Kolkata]