愿你如阳光,明媚不忧伤。
目録
友情链接 【编程规约】日期时间
1. 日期时间 API 概览
java.time 包里有许多可以代表时间和日期的类。这是一个非常丰富的API。
- 类的说明及作用
Date 和 time 做下区分,Date(日期)的单位是年月日。time(时间)的单位是时分秒。
方法 | 说明 |
---|---|
Clock | 时钟类,包含有时区信息 |
DateTimeException | 异常,用于指出计算日期时间时出现的问题 |
DayOfWeek | 枚举类,一周中的7天 |
Duration | 两个时间的差,精确到秒或纳秒 |
Instant | 时间戳(瞬时时间,带时区) |
LocalDate | 日期(比如:2022-02-18,不带时区) |
LocalDateTime | 日期时间(比如:2022-02-18 17:03:10,不带时区) |
LocalTime | 时间(比如:17:03:10,不带时区) |
Month | 月 |
MonthDay | 月日 |
OffsetDateTime | 有偏移的日期时间(例如:2022-02-18T17:03:10+01:00) |
OffsetTime | 有偏移的时间(例如:17:03:10+01:00) |
Peroid | 两个日期的差(精确到日) |
Ser | 此包的共享序列化委托 |
Year | 年 |
YearMonth | 年月 |
ZonedDateTime | 带时区的日期时间 |
ZonedId | 时区 |
ZoneOffset | 时区偏移量(比如:+8:00) |
ZoneRegion | 适用相同时区规则的地理区域 |
2. Clock.class
- 方法示例
一种时钟,提供使用一个时区访问当前的即时、日期和时间,用来代替 System.currentTimeMillis() 和 TimeZone.getDefault()。它是个抽象类,一共有四个子类。
方法 | 说明 |
---|---|
SystemClock | 实现一个时钟,总是返回最近的时间 |
FixedClock | 实现一个时钟总是返回相同的瞬间,通常用于测试 |
OffsetClock | 向基础时钟添加偏移量的时钟的实现 |
TickClock | 向基础时钟添加偏移量的时钟的实现 |
systemUTC | 使用UTC时区转换为日期和时间的时钟 |
systemDefaultZone | 使用默认时区转换为日期和时间的时钟 |
system | 使用指定区域内最佳可用系统时钟的时钟 |
tickMillis | 时间以毫秒为单位的时钟 |
tickSeconds | 时间以秒为单位的时钟 |
tickMinutes | 时间以分钟为单位的时钟 |
tick | 返回一个以基础时钟和时钟记录基础单位为构造的时钟,例如半秒时钟 |
fixed | 只返回指定的瞬间的时钟,确保测试不依赖于当前时钟 |
offset | 返回向基础时钟添加偏移量的时钟 |
getZone | 获取使用的时区 |
withZone | 返回基于此时钟的具有指定时区的时钟 |
millis | 获取时钟的当前毫秒瞬间 |
instant | 获取时间戳(当前时刻) |
*****************************************************************
public class TestDateTime {
public static void main(String[] args) {
// 使用UTC时区的时钟
Clock clockSystemUTC = Clock.systemUTC();
System.out.println("clockSystemUTC\t\t" + clockSystemUTC.instant());
// 使用系统默认时区的时钟
Clock clockSystemDefaultZone = Clock.systemDefaultZone();
System.out.println("clockSystemDefaultZone\t\t" + clockSystemDefaultZone.instant());
// 使用美国凤凰城时区的时钟
Clock clockSystem = Clock.system(ZoneId.of("America/Phoenix"));
System.out.println("clockSystem\t\t" + clockSystem.instant());
// 使用短时区名称(不推荐)CTT - Asia/Shanghai 以毫秒为单位的时钟
Clock clockTickMillis = Clock.tickMillis(ZoneId.of(ZoneId.SHORT_IDS.get("CTT")));
System.out.println("clockTickMillis\t\t" + clockTickMillis.instant());
// 以秒为单位的时钟
Clock clockTickSeconds = Clock.tickSeconds(ZoneId.of("Asia/Shanghai"));
System.out.println("clockTickSeconds\t\t" + clockTickSeconds.instant());
// 以分为单位的时钟
Clock clockTickMinutes = Clock.tickMinutes(ZoneId.of("Asia/Shanghai"));
System.out.println("clockTickMinutes\t\t" + clockTickMinutes.instant());
// 以两天为单位的时钟
Clock clockTick = Clock.tick(clockSystemUTC, Duration.ofDays(2));
System.out.println("clockTick\t\t" + clockTick.instant());
// 固定的时钟
Clock clockFixed = Clock.fixed(clockSystemUTC.instant(), ZoneId.of("Asia/Shanghai"));
System.out.println("clockFixed\t\t" + clockFixed.instant());
// 偏移七天的时钟
Clock clockOffset = Clock.offset(clockSystemUTC, Duration.ofDays(7));
System.out.println("clockOffset\t\t" + clockOffset.instant());
// 获取时钟的时区
ZoneId zoneId = clockSystemUTC.getZone();
System.out.println("zoneId\t\t" + zoneId.getId());
// 获取具有亚洲上海时区的时钟副本
Clock clockWithZone = clockSystemUTC.withZone(ZoneId.of("Asia/Shanghai"));
System.out.println("clockWithZone\t\t" + clockWithZone.instant());
// 获取时钟的当前毫秒瞬间
long millis = clockSystemUTC.millis();
System.out.println("millis\t\t" + millis);
}
}
【Console】-------------------------------------------------------
clockSystemUTC 2022-02-21T11:21:11.451353700Z
clockSystemDefaultZone 2022-02-21T11:21:11.510197500Z
clockSystem 2022-02-21T11:21:11.511194300Z
clockTickMillis 2022-02-21T11:21:11.512Z
clockTickSeconds 2022-02-21T11:21:11Z
clockTickMinutes 2022-02-21T11:21:00Z
clockTick 2022-02-21T00:00:00Z
clockFixed 2022-02-21T11:21:11.514185900Z
clockOffset 2022-02-28T11:21:11.514185900Z
zoneId Z
clockWithZone 2022-02-21T11:21:11.516180600Z
millis 1645442471516
*****************************************************************
3. LocalDateTime.class
- 方法示例
ISO-8601 日历系统中没有时区的日期时间,例如 2007-12-03T10:15:30。这个类是不可变的和线程安全的。
方法 | 说明 |
---|---|
now | 从默认时区的系统时钟获取当前日期时间 |
of | 从年、月、日、小时和分钟获取 LocalDateTime 的实例 |
ofInstant | 从 Instant 和 ZoneId 获取 LocalDateTime 的实例 |
ofEpochSecond | 使用 1970-01-01T00:00:00Z 纪元的秒数获取 LocalDateTime 的实例 |
from | 从时间对象获取 LocalDateTime 的实例 |
parse | 从 2007-12-03T10:15:30 等文本字符串中获取 LocalDateTime 的实例 |
isSupported | 检查是否可以为指定字段查询此日期时间 |
range | 获取指定字段的有效值范围 |
get | 从此日期时间获取指定字段的值作为 int |
getLong | 从此日期时间获取指定字段的值作为 long |
toLocalDate | 获取此日期时间的 LocalDate 部分 |
toLocalTime | 获取此日期时间的 LocalTime 部分 |
getYear | 获取年份字段 |
getMonthValue | 获取从 1 到 12 的年份字段 |
getMonth | 使用 Month 枚举获取月份字段 |
getDayOfMonth | 获取月中第几天 |
getDayOfYear | 获取年中第几天 |
getDayOfWeek | 获取星期字段,它是一个枚举 DayOfWeek |
getHour | 获取小时 |
getMinute | 获取分钟 |
getSecond | 获取秒 |
getNano | 获取纳秒 |
with | 返回此日期时间的调整副本 |
truncatedTo | 返回此 LocalDateTime 的副本,其中时间被截断 |
plus | 返回此日期时间的副本,其中添加了指定的数量 |
minus | 返回此日期时间的副本,其中减去了指定的数量 |
query | 使用指定的查询查询此日期时间 |
adjustInto | 将指定的时间对象调整为与该对象具有相同的日期和时间 |
until | 根据指定的单位计算直到另一个日期时间的时间量 |
format | 使用指定的格式化程序格式化此日期时间 |
atZone | 将此日期时间与时区组合以创建 ZonedDateTime |
atOffset | 将此日期时间与偏移量组合以创建 OffsetDateTime |
compareTo | 将此日期时间与另一个日期时间进行比较 |
isAfter | 检查此日期时间是否在指定的日期时间之后 |
isBefore | 检查此日期时间是否在指定的日期时间之前 |
isEqual | 检查此日期时间是否在指定的日期时间相等 |
*****************************************************************
public class TestDateTime {
public static void main(String[] args) {
// 获取当前本地时间
LocalDateTime nowLocalDateTime = LocalDateTime.now();
System.out.println("nowLocalDateTime\t\t" + nowLocalDateTime);
// 自定义年月日时分秒
LocalDateTime ofLocalDateTime = LocalDateTime.of(2022, 3, 21, 14, 00, 00);
System.out.println("ofLocalDateTime\t\t" + ofLocalDateTime);
// 从当前时间加上时区
LocalDateTime ofInstantLocalDateTime = LocalDateTime.ofInstant(LocalDateTime.now().toInstant(ZoneOffset.UTC), ZoneId.of("America/Chicago"));
System.out.println("ofInstantLocalDateTime\t\t" + ofInstantLocalDateTime);
// 从 1970-01-01T00:00:00Z 纪元开始过后100000纪元秒和1000纳秒的协调世界时
LocalDateTime ofEpochSecondLocalDateTime = LocalDateTime.ofEpochSecond(100000, 1000, ZoneOffset.UTC);
System.out.println("ofEpochSecondLocalDateTime\t\t" + ofEpochSecondLocalDateTime);
// 从时间对象获取 LocalDateTime 的实例
LocalDateTime fromLocalDateTime = LocalDateTime.from(ZonedDateTime.now());
System.out.println("fromLocalDateTime\t\t" + fromLocalDateTime);
// 解析文本字符串
LocalDateTime parseLocalDateTime = LocalDateTime.parse("2018-12-30T12:00:00.00");
System.out.println("parseLocalDateTime\t\t" + parseLocalDateTime);
// 检查是否可以为 DAY_OF_WEEK 查询此日期时间
Boolean isSupportedLocalDateTime = parseLocalDateTime.isSupported(ChronoField.DAY_OF_WEEK);
System.out.println("isSupportedLocalDateTime\t\t" + isSupportedLocalDateTime);
// DAY_OF_MONTH 的范围
ValueRange range = nowLocalDateTime.range(ChronoField.DAY_OF_MONTH);
System.out.println("range\t\t" + range);
// 获取当前日期是一年中的第一天
int getLocalDateTime = nowLocalDateTime.get(ChronoField.DAY_OF_YEAR);
System.out.println("getLocalDateTime\t\t" + getLocalDateTime);
// 将当前日期调整到月底
LocalDateTime withLocalDateTime = nowLocalDateTime.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("withLocalDateTime\t\t" + withLocalDateTime);
// 截断日期到天为止
LocalDateTime truncatedToLocalDateTime = nowLocalDateTime.truncatedTo(ChronoUnit.DAYS);
System.out.println("truncatedToLocalDateTime\t\t" + truncatedToLocalDateTime);
// 当前日期添加十天
LocalDateTime plusLocalDateTime = nowLocalDateTime.plus(Period.ofDays(10));
System.out.println("plusLocalDateTime\t\t" + plusLocalDateTime);
// 当前日期减去十天
LocalDateTime minusLocalDateTime = nowLocalDateTime.minus(Period.ofDays(10));
System.out.println("minusLocalDateTime\t\t" + minusLocalDateTime);
// 查询当前日期时间是否包含本地日期
LocalDate query = nowLocalDateTime.query(TemporalQueries.localDate());
System.out.println("query\t\t" + query);
// 调整当前日期到周三
Temporal adjustIntoLocalDateTime = DayOfWeek.WEDNESDAY.adjustInto(nowLocalDateTime);
System.out.println("adjustIntoLocalDateTime\t\t" + adjustIntoLocalDateTime);
// 获取当前日期到指定日期需要的天数
long until = nowLocalDateTime.until(ofLocalDateTime, ChronoUnit.DAYS);
System.out.println("until\t\t" + until);
// 格式化日期
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
String formatLocalDateTime = nowLocalDateTime.format(formatter);
System.out.println("formatLocalDateTime\t\t" + formatLocalDateTime);
// 创建偏移两个小时的时间
OffsetDateTime atOffsetLocalDateTime = nowLocalDateTime.atOffset(ZoneOffset.ofHours(2));
System.out.println("atOffsetLocalDateTime\t\t" + atOffsetLocalDateTime);
// 创建美国芝加哥时区的时间
ZonedDateTime atZoneLocalDateTime = nowLocalDateTime.atZone(ZoneId.of("America/Chicago"));
System.out.println("atZoneLocalDateTime\t\t" + atZoneLocalDateTime);
// 比较两个时间
int compareToLocalDateTime = withLocalDateTime.compareTo(nowLocalDateTime);
System.out.println("compareToLocalDateTime\t\t" + compareToLocalDateTime);
}
}
【Console】-------------------------------------------------------
nowLocalDateTime 2022-02-21T17:54:48.973290700
ofLocalDateTime 2022-03-21T14:00
ofInstantLocalDateTime 2022-02-21T11:54:48.996229200
ofEpochSecondLocalDateTime 1970-01-02T03:46:40.000001
fromLocalDateTime 2022-02-21T17:54:49.001215600
parseLocalDateTime 2018-12-30T12:00
isSupportedLocalDateTime true
range 1 - 28
getLocalDateTime 52
withLocalDateTime 2022-02-28T17:54:48.973290700
truncatedToLocalDateTime 2022-02-21T00:00
plusLocalDateTime 2022-03-03T17:54:48.973290700
minusLocalDateTime 2022-02-11T17:54:48.973290700
query 2022-02-21
adjustIntoLocalDateTime 2022-02-23T17:54:48.973290700
until 27
formatLocalDateTime 2022-02-21
atOffsetLocalDateTime 2022-02-21T17:54:48.973290700+02:00
atZoneLocalDateTime 2022-02-21T17:54:48.973290700-06:00[America/Chicago]
compareToLocalDateTime 7
*****************************************************************
4. ChronoField.class
- 工具类说明
继承 TemporalField,枚举类。和 ChronoUnit 功能类似,基于TemporalUnit 实现,一般用于获取不同时间域的值。
*****************************************************************
public enum ChronoField implements TemporalField {
NANO_OF_SECOND("NanoOfSecond", NANOS, SECONDS, ValueRange.of(0, 999_999_999)),
NANO_OF_DAY("NanoOfDay", NANOS, DAYS, ValueRange.of(0, 86400L * 1000_000_000L - 1)),
MICRO_OF_SECOND("MicroOfSecond", MICROS, SECONDS, ValueRange.of(0, 999_999)),
MICRO_OF_DAY("MicroOfDay", MICROS, DAYS, ValueRange.of(0, 86400L * 1000_000L - 1)),
MILLI_OF_SECOND("MilliOfSecond", MILLIS, SECONDS, ValueRange.of(0, 999)),
MILLI_OF_DAY("MilliOfDay", MILLIS, DAYS, ValueRange.of(0, 86400L * 1000L - 1)),
SECOND_OF_MINUTE("SecondOfMinute", SECONDS, MINUTES, ValueRange.of(0, 59), "second"),
SECOND_OF_DAY("SecondOfDay", SECONDS, DAYS, ValueRange.of(0, 86400L - 1)),
MINUTE_OF_HOUR("MinuteOfHour", MINUTES, HOURS, ValueRange.of(0, 59), "minute"),
MINUTE_OF_DAY("MinuteOfDay", MINUTES, DAYS, ValueRange.of(0, (24 * 60) - 1)),
HOUR_OF_AMPM("HourOfAmPm", HOURS, HALF_DAYS, ValueRange.of(0, 11)),
CLOCK_HOUR_OF_AMPM("ClockHourOfAmPm", HOURS, HALF_DAYS, ValueRange.of(1, 12)),
HOUR_OF_DAY("HourOfDay", HOURS, DAYS, ValueRange.of(0, 23), "hour"),
CLOCK_HOUR_OF_DAY("ClockHourOfDay", HOURS, DAYS, ValueRange.of(1, 24)),
AMPM_OF_DAY("AmPmOfDay", HALF_DAYS, DAYS, ValueRange.of(0, 1), "dayperiod"),
DAY_OF_WEEK("DayOfWeek", DAYS, WEEKS, ValueRange.of(1, 7), "weekday"),
ALIGNED_DAY_OF_WEEK_IN_MONTH("AlignedDayOfWeekInMonth", DAYS, WEEKS, ValueRange.of(1, 7)),
ALIGNED_DAY_OF_WEEK_IN_YEAR("AlignedDayOfWeekInYear", DAYS, WEEKS, ValueRange.of(1, 7)),
DAY_OF_MONTH("DayOfMonth", DAYS, MONTHS, ValueRange.of(1, 28, 31), "day"),
DAY_OF_YEAR("DayOfYear", DAYS, YEARS, ValueRange.of(1, 365, 366)),
EPOCH_DAY("EpochDay", DAYS, FOREVER, ValueRange.of(-365243219162L, 365241780471L)),
ALIGNED_WEEK_OF_MONTH("AlignedWeekOfMonth", WEEKS, MONTHS, ValueRange.of(1, 4, 5)),
ALIGNED_WEEK_OF_YEAR("AlignedWeekOfYear", WEEKS, YEARS, ValueRange.of(1, 53)),
MONTH_OF_YEAR("MonthOfYear", MONTHS, YEARS, ValueRange.of(1, 12), "month"),
PROLEPTIC_MONTH("ProlepticMonth", MONTHS, FOREVER, ValueRange.of(Year.MIN_VALUE * 12L, Year.MAX_VALUE * 12L + 11)),
YEAR_OF_ERA("YearOfEra", YEARS, FOREVER, ValueRange.of(1, Year.MAX_VALUE, Year.MAX_VALUE + 1)),
YEAR("Year", YEARS, FOREVER, ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE), "year"),
ERA("Era", ERAS, FOREVER, ValueRange.of(0, 1), "era"),
INSTANT_SECONDS("InstantSeconds", SECONDS, FOREVER, ValueRange.of(Long.MIN_VALUE, Long.MAX_VALUE)),
OFFSET_SECONDS("OffsetSeconds", SECONDS, FOREVER, ValueRange.of(-18 * 3600, 18 * 3600));
private final String name;
private final TemporalUnit baseUnit;
private final TemporalUnit rangeUnit;
private final ValueRange range;
private final String displayNameKey;
private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {
this.name = name;
this.baseUnit = baseUnit;
this.rangeUnit = rangeUnit;
this.range = range;
this.displayNameKey = null;
}
private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit,
ValueRange range, String displayNameKey) {
this.name = name;
this.baseUnit = baseUnit;
this.rangeUnit = rangeUnit;
this.range = range;
this.displayNameKey = displayNameKey;
}
}
*****************************************************************
5. ChronoUnit.class
- 工具类说明
时间度量单位,枚举类,继承 TemporalUnit。它表示的是一个时间间隔用什么单位度量,比如两天的时间间隔可以用48个小时代替表示。一般用于某时间单位的设置,加减操作。
*****************************************************************
public enum ChronoUnit implements TemporalUnit {
NANOS("Nanos", Duration.ofNanos(1)),
MICROS("Micros", Duration.ofNanos(1000)),
MILLIS("Millis", Duration.ofNanos(1000_000)),
SECONDS("Seconds", Duration.ofSeconds(1)),
MINUTES("Minutes", Duration.ofSeconds(60)),
HOURS("Hours", Duration.ofSeconds(3600)),
HALF_DAYS("HalfDays", Duration.ofSeconds(43200)),
DAYS("Days", Duration.ofSeconds(86400)),
WEEKS("Weeks", Duration.ofSeconds(7 * 86400L)),
MONTHS("Months", Duration.ofSeconds(31556952L / 12)),
YEARS("Years", Duration.ofSeconds(31556952L)),
DECADES("Decades", Duration.ofSeconds(31556952L * 10L)),
CENTURIES("Centuries", Duration.ofSeconds(31556952L * 100L)),
MILLENNIA("Millennia", Duration.ofSeconds(31556952L * 1000L)),
ERAS("Eras", Duration.ofSeconds(31556952L * 1000_000_000L)),
FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999_999_999));
private final String name;
private final Duration duration;
private ChronoUnit(String name, Duration estimatedDuration) {
this.name = name;
this.duration = estimatedDuration;
}
}
*****************************************************************
6. TemporalAdjuster.class
- 工具类说明
因为 java.time 的时间类都是不可变,因此需要调整时间时,可调用该方法实现。jdk 提供了一些默认调整方法的工具类 TemporalAdjusters。
*****************************************************************
public final class TemporalAdjusters {
// 设置天单位度量数为年的第一天
public static TemporalAdjuster firstDayOfYear()
// 设置天单位度量数为年的最后一天
public static TemporalAdjuster lastDayOfYear()
// 设置时间为下一年的第一天
public static TemporalAdjuster firstDayOfNextYear()
// 设置时间为当月的第一天
public static TemporalAdjuster firstInMonth(DayOfWeek dayOfWeek)
// 设置时间为当月的最后一天
public static TemporalAdjuster lastInMonth(DayOfWeek dayOfWeek)
// 设置时间为当月第ordinal个周的星期几-dayOfWeek
public static TemporalAdjuster dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek)
// 设置时间为下周星期几
public static TemporalAdjuster next(DayOfWeek dayOfWeek)
// 设置时间为上周星期几
public static TemporalAdjuster previous(DayOfWeek dayOfWeek)
// 如果当前星期数和dayOfWeek不一样,则设置时间为上周的星期几-dayOfWeek
public static TemporalAdjuster previousOrSame(DayOfWeek dayOfWeek)
}
*****************************************************************
还有很多就不在此列举了,累了…