为了更好地理解,让我们把这些方法分类为它们的用法,并举例加以说明。
static ZonedDateTime now()
- 从默认时区的系统时钟中获取当前日期时间。static ZonedDateTime now(Clock clock)
- 从指定的时钟获取当前的日期时间。static ZonedDateTime now(ZoneId zone)
- 从指定时区的系统时钟获取当前的日期时间。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的一个实例。LocalDate toLocalDate()
- 获取该日期时间的LocalDate部分。LocalDateTime toLocalDateTime()
- 获取该日期时间的LocalDateTime部分。LocalTime toLocalTime()
- 获取此日期时间的LocalTime部分。OffsetDateTime toOffsetDateTime()
- 将此日期时间转换为OffsetDateTime。int getDayOfMonth()
- 获取月日字段。int getDayOfYear()
- 获取年月日字段。int getMonthValue()
- 获取1至12的年月日字段。ZoneOffset getOffset()
- 获取区域偏移,如'+01:00'。int getYear()
- 获取年份字段。int getHour()
- 获取小时-日字段。int getMinute()
- 获取小时中的分钟字段。int getSecond()
- 获得分钟的第二个字段。int getNano()
- 获取纳秒字段。ZonedDateTime plusDays(long days)
- 返回这个ZonedDateTime的副本,并添加指定的天数。ZonedDateTime plusHours(long hours)
- 返回此ZonedDateTime的副本,并添加指定的小时数。ZonedDateTime plusMinutes(long minutes)
- 返回此ZonedDateTime的副本,并添加指定的分钟数。ZonedDateTime plusMonths(long months)
- 返回此ZonedDateTime的副本,并添加指定的月数。ZonedDateTime plusNanos(long nanos)
- 返回此ZonedDateTime的副本,并添加指定的纳秒数。ZonedDateTime plusSeconds(long seconds)
- 返回此ZonedDateTime的副本,并添加指定的秒数。ZonedDateTime plusWeeks(long weeks)
- 返回此ZonedDateTime的副本,并添加指定的周数。ZonedDateTime plusYears(long years)
- 返回此ZonedDateTime的副本,并添加指定的年数。ZonedDateTime minusDays(long days)
- 返回此ZonedDateTime的副本,并减去指定的天数。ZonedDateTime minusHours(long hours)
- 返回此ZonedDateTime的副本,并减去指定的小时数。ZonedDateTime minusMinutes(long minutes)
- 返回此ZonedDateTime的副本,并减去指定的分钟数。ZonedDateTime minusMonths(long months)
- 返回此ZonedDateTime的副本,并减去指定的月数。ZonedDateTime minusSeconds(long seconds)
- 返回这个ZonedDateTime的副本,并减去指定的秒数。ZonedDateTime minusWeeks(long weeks)
- 返回此ZonedDateTime的副本,并减去指定的星期数。ZonedDateTime minusYears(long years)
- 返回这个ZonedDateTime的副本,并减去指定的年数。从接口java.time.Chrono.ChronoZonedDateTime继承的方法
default boolean isAfter(ChronoZonedDateTime<?> other)
- 检查这个日期时间的瞬间是否在指定日期时间之后。default boolean isBefore(ChronoZonedDateTime<?> other)
- 检查这个日期时间的瞬间是否在指定日期时间之前。default boolean isEqual(ChronoZonedDateTime<?> other)
- 检查此日期时间的时刻是否等于指定日期时间的时刻。default int compareTo(ChronoZonedDateTime<?> other)
- 将此日期时间与另一日期时间相比较,包括时间顺序。static ZonedDateTime parse(CharSequence text)
- 从文本字符串中获取ZonedDateTime的一个实例,如2007-12-03T10:15:30+01:00[欧洲/巴黎]。static ZonedDateTime parse(CharSequence text, DateTimeFormatter formatter)
- 从使用特定格式的文本字符串中获取ZonedDateTime的实例。String format(DateTimeFormatter formatter)
- 使用指定的格式化器对该日期时间进行格式化。ZonedDateTime类提供了以下的API来创建带有区域信息的当前和特定的日期时间对象,如下所示。
static ZonedDateTime now()
- 从默认时区的系统时钟获取当前日期时间。static ZonedDateTime now(Clock clock)
- 从指定的时钟获取当前的日期时间。static ZonedDateTime now(ZoneId zone)
- 从指定时区的系统时钟获取当前的日期时间。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的一个实例。import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
//*/*
/* Program to demonstrate ZonedDateTime Class APIs.
/* @author javaguides.net
/*
/*/
public class ZonedDateTimeExample {
public static void main(String[] args) {
createZonedDateTime();
}
private static void createZonedDateTime() {
// Current date time
ZonedDateTime dateTime1 = ZonedDateTime.now();
System.out.println(dateTime1);
// Current date time from specified time-zone
ZonedDateTime dateTime2 = ZonedDateTime.now(ZoneId.of("UTC"));
System.out.println(dateTime2);
// Current date time from specified clock
ZonedDateTime dateTime3 = ZonedDateTime.now(Clock.systemDefaultZone());
System.out.println(dateTime3);
// Current zoned date time from LocalDateTime
ZonedDateTime dateTime4 = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("GMT"));
System.out.println(dateTime4);
// Specific zoned date time from LocalDateTime
ZonedDateTime dateTime5 = ZonedDateTime.of(LocalDateTime.of(2017, 05, 12, 05, 45), ZoneId.of("Europe/London"));
System.out.println(dateTime5);
}
}
输出。
2018-08-11T11:15:37.717+05:30[Asia/Calcutta]
2018-08-11T05:45:37.718Z[UTC]
2018-08-11T11:15:37.718+05:30[Asia/Calcutta]
2018-08-11T11:15:37.718Z[GMT]
2017-05-12T05:45+01:00[Europe/London]
ZonedDateTime类提供了以下API,用于从ZonedDateTime类获取LocalDateTime、LocalDate、LocalTime、OffsetDateTime和Instant的实例。
LocalDate toLocalDate()
- 获取该日期时间的LocalDate部分。LocalDateTime toLocalDateTime()
- 获取该日期时间的LocalDateTime部分。LocalTime toLocalTime()
- 获取此日期时间的LocalTime部分。OffsetDateTime toOffsetDateTime()
- 将此日期时间转换为OffsetDateTime。import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
//*/*
/* Program to demonstrate ZonedDateTime Class APIs.
/* @author javaguides.net
/*
/*/
public class ZonedDateTimeExample {
public static void main(String[] args) {
getInstances();
}
private static void getInstances() {
// Current date time with zone
ZonedDateTime dateTime = ZonedDateTime.now();
System.out.println(dateTime);
// Get LocalDateTime
LocalDateTime localDateTime = dateTime.toLocalDateTime();
System.out.println(localDateTime);
// Get LocalDate
LocalDate localDate = dateTime.toLocalDate();
System.out.println(localDate);
// Get LocalTime
LocalTime localTime = dateTime.toLocalTime();
System.out.println(localTime);
// Get OffsetDateTime
OffsetDateTime offsetDateTime = dateTime.toOffsetDateTime();
System.out.println(offsetDateTime);
// Get Instant
Instant instant = dateTime.toInstant();
System.out.println(instant);
}
}
输出。
2018-08-11T11:19:18.902+05:30[Asia/Calcutta]
2018-08-11T11:19:18.902
2018-08-11
11:19:18.902
2018-08-11T11:19:18.902+05:30
2018-08-11T05:49:18.902Z
ZonedDateTime类提供以下API,从ZonedDateTime类中获取年、月、日。
int getDayOfMonth()
- 获取月中的日字段。int getDayOfYear()
- 获得年的日期字段。int getMonthValue()
- 获取1到12的年月日字段。ZoneOffset getOffset()
- 获取区域偏移,如'+01:00'。int getYear()
- 获得年字段。import java.time.ZonedDateTime;
//*/*
/* Program to demonstrate ZonedDateTime Class APIs.
/* @author javaguides.net
/*
/*/
public class ZonedDateTimeExample {
public static void main(String[] args) {
getYearMonthDayfromZonedDateTime();
}
private static void getYearMonthDayfromZonedDateTime() {
ZonedDateTime dateTime = ZonedDateTime.now();
System.out.println("Year : " + dateTime.getYear());
System.out.println("Month : " + dateTime.getMonth().getValue());
System.out.println("Day of Month : " + dateTime.getDayOfMonth());
System.out.println("Day of Week : " + dateTime.getDayOfWeek());
System.out.println("Day of Year : " + dateTime.getDayOfYear());
System.out.println("Zone Id : " + dateTime.getZone());
System.out.println("Offset : " + dateTime.getOffset());
}
}
输出。
Year : 2018
Month : 8
Day of Month : 11
Day of Week : SATURDAY
Day of Year : 223
Zone Id : Asia/Calcutta
Offset : +05:30
ZonedDateTime类提供以下API,从ZonedDateTime类中获得小时、分钟、秒。
int getHour()
- 获取一天中的小时字段。int getMinute()
- 获取小时中的分钟字段。int getSecond()
- 获得分钟的第二个字段。int getNano()
- 获得秒的纳米字段。import java.time.ZonedDateTime;
//*/*
/* Program to demonstrate ZonedDateTime Class APIs.
/* @author javaguides.net
/*
/*/
public class ZonedDateTimeExample {
public static void main(String[] args) {
getHourMinuteSecondfromZonedDateTime();
}
private static void getHourMinuteSecondfromZonedDateTime() {
ZonedDateTime dateTime = ZonedDateTime.now();
System.out.println("Hour : " + dateTime.getHour());
System.out.println("Minute : " + dateTime.getMinute());
System.out.println("Second : " + dateTime.getSecond());
System.out.println("Nano : " + dateTime.getNano());
}
}
输出。
Hour : 11
Minute : 23
Second : 8
Nano : 5000000
ZonedDateTime类提供了以下的API来增加或减少ZonedDateTime类的年、月、日、时、分和秒。
ZonedDateTime plusDays(long days)
- 返回这个ZonedDateTime的副本,并添加指定的天数。ZonedDateTime plusHours(long hours)
- 返回此ZonedDateTime的副本,并添加指定的小时数。ZonedDateTime plusMinutes(long minutes)
- 返回此ZonedDateTime的副本,并添加指定的分钟数。ZonedDateTime plusMonths(long months)
- 返回此ZonedDateTime的副本,并添加指定的月数。ZonedDateTime plusNanos(long nanos)
- 返回此ZonedDateTime的副本,并添加指定的纳秒数。ZonedDateTime plusSeconds(long seconds)
- 返回此ZonedDateTime的副本,并添加指定的秒数。ZonedDateTime plusWeeks(long weeks)
- 返回此ZonedDateTime的副本,并添加指定的周数。ZonedDateTime plusYears(long years)
- 返回此ZonedDateTime的副本,并添加指定的年数。ZonedDateTime minusDays(long days)
- 返回此ZonedDateTime的副本,并减去指定的天数。ZonedDateTime minusHours(long hours)
- 返回此ZonedDateTime的副本,并减去指定的小时数。ZonedDateTime minusMinutes(long minutes)
- 返回此ZonedDateTime的副本,并减去指定的分钟数。ZonedDateTime minusMonths(long months)
- 返回这个ZonedDateTime的副本,并减去指定的月数。ZonedDateTime minusSeconds(long seconds)
- 返回此ZonedDateTime的副本,并减去指定的秒数。ZonedDateTime minusWeeks(long weeks)
- 返回这个ZonedDateTime的副本,并减去指定的星期数。ZonedDateTime minusYears(long years)
- 返回这个ZonedDateTime的副本,并减去指定的年数。import java.time.ZoneId;
import java.time.ZonedDateTime;
//*/*
/* Program to demonstrate ZonedDateTime Class APIs.
/* @author javaguides.net
/*
/*/
public class ZonedDateTimeExample {
public static void main(String[] args) {
addorSubtractZonedDateTime();
}
private static void addorSubtractZonedDateTime() {
ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
// LocalDateTime's plus methods
System.out.println("Addition of days : " + dateTime.plusDays(5));
System.out.println("Addition of months : " + dateTime.plusMonths(15));
System.out.println("Addition of years : " + dateTime.plusYears(5));
System.out.println("Addition of Hours : " + dateTime.plusHours(2));
System.out.println("Addition of Minutes : " + dateTime.plusMinutes(30));
System.out.println("Addition of Seconds : " + dateTime.plusSeconds(20));
System.out.println("Addition of Weeks : " + dateTime.plusWeeks(2));
System.out.println("Addition of Nano : " + dateTime.plusNanos(2000));
// LocalDateTime's minus methods
System.out.println("Subtraction of days : " + dateTime.minusDays(5));
System.out.println("Subtraction of months : " + dateTime.minusMonths(15));
System.out.println("Subtraction of years : " + dateTime.minusYears(5));
System.out.println("Subtraction of Hours : " + dateTime.minusHours(2));
System.out.println("Subtraction of Minutes : " + dateTime.minusMinutes(30));
System.out.println("Subtraction of Seconds : " + dateTime.minusSeconds(20));
System.out.println("Subtraction of Weeks : " + dateTime.minusWeeks(2));
System.out.println("Subtraction of Nano : " + dateTime.minusNanos(2000));
}
}
输出。
Addition of days : 2018-08-16T01:54:33.434-04:00[America/New_York]
Addition of months : 2019-11-11T01:54:33.434-05:00[America/New_York]
Addition of years : 2023-08-11T01:54:33.434-04:00[America/New_York]
Addition of Hours : 2018-08-11T03:54:33.434-04:00[America/New_York]
Addition of Minutes : 2018-08-11T02:24:33.434-04:00[America/New_York]
Addition of Seconds : 2018-08-11T01:54:53.434-04:00[America/New_York]
Addition of Weeks : 2018-08-25T01:54:33.434-04:00[America/New_York]
Addition of Nano : 2018-08-11T01:54:33.434002-04:00[America/New_York]
Subtraction of days : 2018-08-06T01:54:33.434-04:00[America/New_York]
Subtraction of months : 2017-05-11T01:54:33.434-04:00[America/New_York]
Subtraction of years : 2013-08-11T01:54:33.434-04:00[America/New_York]
Subtraction of Hours : 2018-08-10T23:54:33.434-04:00[America/New_York]
Subtraction of Minutes : 2018-08-11T01:24:33.434-04:00[America/New_York]
Subtraction of Seconds : 2018-08-11T01:54:13.434-04:00[America/New_York]
Subtraction of Weeks : 2018-07-28T01:54:33.434-04:00[America/New_York]
Subtraction of Nano : 2018-08-11T01:54:33.433998-04:00[America/New_York]
ZonedDateTime类提供了以下API来比较Java中的ZonedDateTime对象。
从java.time.Chrono.ChronoZonedDateTime接口继承的方法
default boolean isAfter(ChronoZonedDateTime<?> other)
- 检查这个日期时间的瞬间是否在指定日期时间之后。default boolean isBefore(ChronoZonedDateTime<?> other)
- 检查此日期时间的时刻是否在指定日期时间之前。default boolean isEqual(ChronoZonedDateTime<?> other)
- 检查这个日期时间的时刻是否等于指定日期时间的时刻。default int compareTo(ChronoZonedDateTime<?> other)
- 将此日期时间与另一日期时间相比较,包括时间顺序。import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
//*/*
/* Program to demonstrate ZonedDateTime Class APIs.
/* @author javaguides.net
/*
/*/
public class ZonedDateTimeExample {
public static void main(String[] args) {
compareZonedDateTimeObjects();
}
private static void compareZonedDateTimeObjects() {
LocalDateTime dateTime = LocalDateTime.now();
ZonedDateTime dt1 = ZonedDateTime.of(dateTime, ZoneId.of("America/New_York"));
ZonedDateTime dt2 = ZonedDateTime.of(dateTime, ZoneId.of("America/New_York"));
ZonedDateTime dt3 = ZonedDateTime.of(dateTime, ZoneId.of("UTC"));
// Using isEqual()
if (dt1.isEqual(dt2)) {
System.out.println("dateTime1 and dateTime2 are equal.");
} else {
System.out.println("dateTime1 and dateTime2 are not equal.");
}
// Using compareTo()
if (dt1.compareTo(dt2) == 0) {
System.out.println("dateTime1 and dateTime2 are equal.");
} else {
System.out.println("dateTime1 and dateTime2 are not equal.");
}
// Using isAfter()
if (dt2.isAfter(dt3)) {
System.out.println("dateTime2 is after dateTime3");
}
// Using isBefore()
if (dt3.isBefore(dt1)) {
System.out.println("dateTime3 is before dateTime1");
}
}
}
输出。
dateTime1 and dateTime2 are equal.
dateTime1 and dateTime2 are equal.
dateTime2 is after dateTime3
dateTime3 is before dateTime1
ZonedDateTime类提供以下API,用于在java中转换或解析字符串为ZonedDateTime。
static ZonedDateTime parse(CharSequence text)
- 从一个文本字符串中获得一个ZonedDateTime的实例,如2007-12-03T10:15:30+01:00[欧洲/巴黎]。static ZonedDateTime parse(CharSequence text, DateTimeFormatter formatter)
- 使用特定的格式从文本字符串中获取ZonedDateTime的实例。import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
//*/*
/* Program to demonstrate ZonedDateTime Class APIs.
/* @author javaguides.net
/*
/*/
public class ZonedDateTimeExample {
public static void main(String[] args) {
convertOrParseStringtoZonedDateTime();
}
private static void convertOrParseStringtoZonedDateTime() {
// ISO date time
ZonedDateTime dt1 = ZonedDateTime.parse("2017-03-28T12:25:38.492+05:30[Asia/Calcutta]",
DateTimeFormatter.ISO_ZONED_DATE_TIME);
System.out.println(dt1);
// 'yyyy-MMM-dd HH:mm:ss' pattern
ZonedDateTime dt2 = ZonedDateTime.parse("2017-May-02 23:35:05",
DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss").withZone(ZoneId.of("UTC")));
System.out.println(dt2);
// 'yyyy-MM-dd KK:mm:ss a' pattern
ZonedDateTime dt3 = ZonedDateTime.parse("2017-05-30 10:20:30 AM",
DateTimeFormatter.ofPattern("yyyy-MM-dd KK:mm:ss a").withZone(ZoneId.systemDefault()));
System.out.println(dt3);
// 'cccc, MMMM dd, yyyy KK:mm a' pattern
ZonedDateTime dt4 = ZonedDateTime.parse("Wednesday, May 31, 2017 10:21 PM",
DateTimeFormatter.ofPattern("cccc, MMMM dd, yyyy KK:mm a").withZone(ZoneId.of("Europe/Paris")));
System.out.println(dt4);
}
}
输出。
2017-03-28T12:25:38.492+05:30[Asia/Calcutta]
2017-05-02T23:35:05Z[UTC]
2017-05-30T10:20:30+05:30[Asia/Calcutta]
2017-05-31T22:21+02:00[Europe/Paris]
ZonedDateTime类提供以下API,用于在java中转换或格式化ZonedDateTime为字符串。
String format(DateTimeFormatter formatter)
- 使用指定的格式化器对该日期时间进行格式化。import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
//*/*
/* Program to demonstrate ZonedDateTime Class APIs.
/* @author javaguides.net
/*
/*/
public class ZonedDateTimeExample {
public static void main(String[] args) {
convertorformatZonedDateTimetoString();
}
private static void convertorformatZonedDateTimetoString() {
// ISO pattern
ZonedDateTime dateTime = ZonedDateTime.now();
System.out.println(dateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// 'yyyy-MMM-dd HH:mm:ss z' pattern
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss z")));
// 'yyyy-MM-dd KK:mm:ss a' pattern
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd KK:mm:ss a")));
// 'cccc, MMMM dd, yyyy KK:mm a' pattern
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("cccc, MMMM dd, yyyy KK:mm a")));
}
}
输出。
2018-08-11T11:31:27.531+05:30[Asia/Calcutta]
2018-Aug-11 11:31:27 IST
2018-08-11 11:31:27 AM
Saturday, August 11, 2018 11:31 AM
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/08/java-8-zoneddatetime-class-api-guide.html
内容来源于网络,如有侵权,请联系作者删除!