Java教程-Java OffsetDateTime类

Java OffsetDateTime类
Java OffsetDateTime类是一个不可变的日期时间对象,表示带有偏移量的日期时间。它继承自Object类并实现了Comparable接口。
OffsetDateTime类用于存储日期和时间字段,精确到纳秒。
Java OffsetDateTime类声明
让我们来看一下java.time.OffsetDateTime类的声明。
- public final class OffsetDateTime extends Object
- implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable
Java OffsetDateTime类的方法
方法 | 描述 |
---|---|
int get(TemporalField field) | 用于获取此日期时间的指定字段的值,返回int类型。 |
int getDayOfMonth() | 获取月份中的天数。 |
int getDayOfYear() | 获取年份中的天数。 |
DayOfWeek getDayOfWeek() | 获取星期几的枚举值DayOfWeek。 |
OffsetDateTime minusDays(long days) | 返回减去指定天数后的OffsetDateTime的副本。 |
static OffsetDateTime now() | 获取当前日期时间,使用系统时钟和默认时区。 |
OffsetDateTime plusDays(long days) | 返回添加指定天数后的OffsetDateTime的副本。 |
LocalDate toLocalDate() | 获取OffsetDateTime的本地日期部分。 |
Temporal adjustInto(Temporal temporal) | 调整指定的Temporal对象,使其具有与此对象相同的日期和时间。 |
ZonedDateTime atZoneSameInstant(ZoneId zone) | 将此日期时间与时区组合,创建一个ZonedDateTime,确保结果具有相同的瞬时点。 |
ZonedDateTime atZoneSimilarLocal(ZoneId zone) | 将此日期时间与时区组合,创建一个ZonedDateTime,尽量保持相同的本地日期和时间。 |
int compareTo(OffsetDateTime other) | 将此日期时间与另一个日期时间进行比较。 |
boolean equals(Object obj) | 检查此日期时间是否与另一个日期时间相等。 |
String format(DateTimeFormatter formatter) | 使用指定的格式化程序对此日期时间进行格式化。 |
static OffsetDateTime from(TemporalAccessor temporal) | 从Temporal对象获取OffsetDateTime的实例。 |
int getHour() | 获取一天中的小时数。 |
long getLong(TemporalField field) | 以long类型获取此日期时间的指定字段的值。 |
int getMinute() | 获取小时中的分钟数。 |
Month getMonth() | 使用Month枚举类型获取年份中的月份。 |
int getMonthValue() | 获取年份中的月份,范围从1到12。 |
int getNano() | 获取秒的纳秒部分。 |
ZoneOffset getOffset() | 获取时区偏移量,例如"+01:00"。 |
int getSecond() | 获取分钟中的秒数。 |
int getYear() | 获取年份。 |
int hashCode() | 返回此日期时间的哈希码。 |
boolean isAfter(OffsetDateTime other) | 检查此日期时间是否在指定日期时间之后。 |
boolean isBefore(OffsetDateTime other) | 检查此日期时间是否在指定日期时间之前。 |
boolean isEqual(OffsetDateTime other) | 检查此日期时间是否与指定日期时间相等。 |
boolean isSupported(TemporalField field) | 检查是否支持指定的字段。 |
boolean isSupported(TemporalUnit unit) | 检查是否支持指定的单位。 |
OffsetDateTime minus(long amountToSubtract, TemporalUnit unit) | 返回减去指定数量的时间单位后的OffsetDateTime的副本。 |
OffsetDateTime minus(TemporalAmount amountToSubtract) | 返回减去指定数量的时间量后的OffsetDateTime的副本。 |
OffsetDateTime minusMonths(long monthsToSubtract) | 返回减去指定数量的月份后的OffsetDateTime的副本。 |
OffsetDateTime minusHours(long hoursToSubtract) | 返回减去指定数量的小时后的OffsetDateTime的副本。 |
OffsetDateTime minusMinutes(long minutesToSubtract) | 返回减去指定数量的分钟后的OffsetDateTime的副本。 |
OffsetDateTime minusNanos(long nanos) | 返回减去指定数量的纳秒后的OffsetDateTime的副本。 |
static OffsetDateTime of(LocalDate date, LocalTime time, ZoneOffset offset) | 从日期、时间和偏移量获取OffsetDateTime的实例。 |
static OffsetDateTime ofInstant(Instant instant, ZoneId zone) | 从Instant和时区ID获取OffsetDateTime的实例。 |
static OffsetDateTime parse(CharSequence text) | 从文本字符串(如2007-12-03T10:15:30)获取OffsetDateTime的实例。 |
ValueRange range(TemporalField field) | 获取指定字段的有效值范围。 |
long toEpochSecond() | 将此日期时间转换为1970-01-01T00:00:00Z以来的秒数。 |
OffsetDateTime truncatedTo(TemporalUnit unit) | 返回将时间截断为指定单位的OffsetDateTime的副本。 |
OffsetDateTime with(TemporalAdjuster adjuster) | 返回此日期时间的调整副本。 |
Java OffsetDateTime类示例:getDayOfMonth()
OffsetDateTimeExample1.java
import java.time.OffsetDateTime;
public class OffsetDateTimeExample1 {
public static void main(String[] args) {
OffsetDateTime offsetDT = OffsetDateTime.now();
System.out.println(offsetDT.getDayOfMonth());
}
}
输出:
18
Java OffsetDateTime类示例:getDayOfYear()
OffsetDateTimeExample2.java
import java.time.OffsetDateTime;
public class OffsetDateTimeExample2 {
public static void main(String[] args) {
OffsetDateTime offsetDT = OffsetDateTime.now();
System.out.println(offsetDT.getDayOfYear());
}
}
输出:
18
Java OffsetDateTime类示例:getDayOfWeek()
OffsetDateTimeExample3.java
import java.time.OffsetDateTime;
public class OffsetDateTimeExample3 {
public static void main(String[] args) {
OffsetDateTime offsetDT = OffsetDateTime.now();
System.out.println(offsetDT.getDayOfWeek());
}
}
输出:
WEDNESDAY
Java OffsetDateTime类示例:toLocalDate()
OffsetDateTimeExample4.java
import java.time.OffsetDateTime;
public class OffsetDateTimeExample4 {
public static void main(String[] args) {
OffsetDateTime offsetDT = OffsetDateTime.now();
System.out.println(offsetDT.toLocalDate());
}
}
输出:
2017-01-18
Java OffsetDateTime类示例:minusDays()
OffsetDateTimeExample5.java
import java.time.OffsetDateTime;
public class OffsetDateTimeExample5 {
public static void main(String[] args) {
OffsetDateTime offset = OffsetDateTime.now();
OffsetDateTime value = offset.minusDays(240);
System.out.println(value);
}
}
输出:
2016-05-23T12:12:31.642+05:30
Java OffsetDateTime类示例:plusDays()
OffsetDateTimeExample6.java
import java.time.OffsetDateTime;
public class OffsetDateTimeExample6 {
public static void main(String[] args) {
OffsetDateTime offset = OffsetDateTime.now();
OffsetDateTime value = offset.plusDays(240);
System.out.println(value);
}
}
输出:
2017-09-15T13:50:30.526+05:30