旅游网站排名前5位的,怎么样做网站 用网站赚钱,网络营销的5种方式,黑龙江新闻法制频道时间漫步者#xff1a;深入Java时间库实战
前言
在现代软件开发中#xff0c;处理日期与时间是一个常见而又具有挑战性的任务。Java为我们提供了强大的日期与时间处理库#xff0c;这些库不仅使日期与时间的操作更加方便#xff0c;而且满足了各种复杂的需求。本文将深入…时间漫步者深入Java时间库实战
前言
在现代软件开发中处理日期与时间是一个常见而又具有挑战性的任务。Java为我们提供了强大的日期与时间处理库这些库不仅使日期与时间的操作更加方便而且满足了各种复杂的需求。本文将深入研究几个重要的Java日期与时间处理库探索它们的功能、用法以及如何在实际项目中灵活运用。 欢迎订阅专栏Java万花筒 文章目录 时间漫步者深入Java时间库实战前言1. java.time 包1.1 LocalDate1.2 LocalTime1.3 LocalDateTime1.4 Instant1.5 Period 与 Duration1.6 DateTimeFormatter1.7 TemporalAdjusters1.8 ZoneId 与 ZoneOffset1.9 Clock1.10 YearMonth 与 MonthDay 2. Joda-Time2.1 DateTime 类2.2 Interval2.3 Period 与 Duration2.4 DateTimeFormatter2.5 DateTimeZone2.6 LocalDate、LocalTime、LocalDateTime2.7 ISOChronology2.8 PeriodType2.9 MutableDateTime 3. ThreeTen-Extra3.1 Interval3.2 YearQuarter3.3 YearWeek3.4 与时间相关的工具3.5 YearQuarterRange3.6 YearQuarterAdjusters3.7 YearQuarterIterator3.8 YearQuarterRangeSet3.9 YearQuarterSerializer3.10 YearQuarterParser 4. Apache Commons Lang4.1 DateUtils4.2 DurationFormatUtils4.3 StopWatch4.4 FastDateFormat4.5 DateUtils解析字符串为日期4.6 DurationFormatUtils格式化毫秒为友好格式4.7 StopWatch记录多个阶段的时间4.8 FastDateFormat解析字符串为日期4.9 StopWatch获取总时间和各阶段时间 5. ChronoUnit5.1 表示时间单位的枚举5.2 用于日期和时间计算的工具5.3 常见的 ChronoUnit5.4 ChronoUnit 的 Plus 和 Minus 操作5.5 使用 ChronoUnit 进行时间戳的计算5.6 通过 ChronoUnit 计算日期调整 总结 1. java.time 包
1.1 LocalDate
LocalDate 类表示日期。它提供了处理日期的方法例如获取年、月、日等。
LocalDate currentDate LocalDate.now();
int year currentDate.getYear();
int month currentDate.getMonthValue();
int day currentDate.getDayOfMonth();1.2 LocalTime
LocalTime 类表示时间。可以使用它获取当前时间或者指定小时、分钟和秒。
LocalTime currentTime LocalTime.now();
int hour currentTime.getHour();
int minute currentTime.getMinute();
int second currentTime.getSecond();1.3 LocalDateTime
LocalDateTime 类表示日期和时间。它结合了 LocalDate 和 LocalTime 的功能。
LocalDateTime currentDateTime LocalDateTime.now();
int year currentDateTime.getYear();
int month currentDateTime.getMonthValue();
int day currentDateTime.getDayOfMonth();
int hour currentDateTime.getHour();
int minute currentDateTime.getMinute();
int second currentDateTime.getSecond();1.4 Instant
Instant 类表示时间戳。可以用于表示一个特定的时间点。
Instant timestamp Instant.now();
long epochSeconds timestamp.getEpochSecond();1.5 Period 与 Duration
Period 用于表示日期之间的差距Duration 用于表示时间之间的差距。
LocalDate startDate LocalDate.of(2022, 1, 1);
LocalDate endDate LocalDate.of(2023, 1, 1);
Period period Period.between(startDate, endDate);LocalTime startTime LocalTime.of(12, 0);
LocalTime endTime LocalTime.of(18, 0);
Duration duration Duration.between(startTime, endTime);1.6 DateTimeFormatter
DateTimeFormatter 类用于将日期和时间对象格式化为字符串或将字符串解析为日期和时间对象。
// 格式化日期
LocalDate dateToFormat LocalDate.of(2023, 5, 15);
DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy-MM-dd);
String formattedDate dateToFormat.format(formatter);// 解析字符串为日期
String dateString 2023-05-15;
LocalDate parsedDate LocalDate.parse(dateString, formatter);1.7 TemporalAdjusters
TemporalAdjusters 类提供了许多静态方法用于对日期进行调整。例如可以获取某月的第一个星期一。
// 获取下一个周一
LocalDate today LocalDate.now();
LocalDate nextMonday today.with(TemporalAdjusters.next(DayOfWeek.MONDAY));1.8 ZoneId 与 ZoneOffset
ZoneId 表示时区而 ZoneOffset 表示与协调世界时UTC的时差。
// 获取当前时区
ZoneId currentZone ZoneId.systemDefault();
System.out.println(当前时区 currentZone);// 获取指定时区的时间
ZoneId newYorkZone ZoneId.of(America/New_York);
ZonedDateTime newYorkTime ZonedDateTime.now(newYorkZone);1.9 Clock
Clock 类提供了对当前时间和日期的访问。可以指定时区来获取特定时区的时间。
// 获取系统默认时钟
Clock systemClock Clock.systemDefaultZone();
Instant instant Instant.now(systemClock);// 获取指定时区的时钟
Clock newYorkClock Clock.system(ZoneId.of(America/New_York));
Instant newYorkInstant Instant.now(newYorkClock);1.10 YearMonth 与 MonthDay
YearMonth 表示年份和月份而 MonthDay 表示月份和日。
// 获取当前年月
YearMonth currentYearMonth YearMonth.now();
System.out.println(当前年月 currentYearMonth);// 获取特定月份和日
MonthDay specificMonthDay MonthDay.of(8, 15);
System.out.println(特定月日 specificMonthDay);2. Joda-Time
2.1 DateTime 类
DateTime 类是 Joda-Time 提供的主要类用于表示日期和时间。
DateTime currentDateTime new DateTime();
int year currentDateTime.getYear();
int month currentDateTime.getMonthOfYear();
int day currentDateTime.getDayOfMonth();2.2 Interval
Interval 类表示两个时间点之间的时间间隔。
DateTime start new DateTime(2022, 1, 1, 0, 0);
DateTime end new DateTime(2023, 1, 1, 0, 0);
Interval interval new Interval(start, end);2.3 Period 与 Duration
Joda-Time 也提供了 Period 和 Duration 用于表示日期和时间的差距。
DateTime startDate new DateTime(2022, 1, 1, 0, 0);
DateTime endDate new DateTime(2023, 1, 1, 0, 0);
Period period new Period(startDate, endDate);DateTime startTime new DateTime(0, 1, 1, 12, 0);
DateTime endTime new DateTime(0, 1, 1, 18, 0);
Duration duration new Duration(startTime, endTime);2.4 DateTimeFormatter
Joda-Time 提供了类似于Java 8中DateTimeFormatter的格式化工具。
// 格式化日期
DateTimeFormatter formatter DateTimeFormat.forPattern(yyyy-MM-dd);
DateTime dateTimeToFormat DateTime.now();
String formattedDate dateTimeToFormat.toString(formatter);// 解析字符串为日期
String dateString 2023-05-15;
DateTime parsedDateTime DateTime.parse(dateString, formatter);2.5 DateTimeZone
DateTimeZone 类用于表示时区并提供了各种时区的支持。
// 获取默认时区
DateTimeZone defaultZone DateTimeZone.getDefault();
System.out.println(默认时区 defaultZone);// 获取特定时区
DateTimeZone newYorkZone DateTimeZone.forID(America/New_York);
DateTime newYorkDateTime DateTime.now(newYorkZone);2.6 LocalDate、LocalTime、LocalDateTime
Joda-Time 同样提供了用于处理日期、时间以及日期时间的类。
// 获取当前日期
LocalDate jodaDate LocalDate.now();// 获取当前时间
LocalTime jodaTime LocalTime.now();// 获取当前日期时间
LocalDateTime jodaDateTime LocalDateTime.now();2.7 ISOChronology
ISOChronology 是 Joda-Time 中用于处理ISO8601标准的实现提供了对于日期和时间的丰富支持。
// 使用ISOChronology
Chronology isoChronology ISOChronology.getInstance();
DateTime isoDateTime new DateTime(2023, 5, 15, 14, 30, 0, isoChronology);2.8 PeriodType
PeriodType 枚举用于定义 Period 对象的字段集合例如年、月、日等。
// 创建自定义PeriodType
PeriodType customPeriodType PeriodType.yearDayTime().withMillisRemoved();
Period customPeriod new Period(2, 3, 0, 0, 0, 0, 0, 0, customPeriodType);2.9 MutableDateTime
MutableDateTime 类是 DateTime 类的可变版本允许直接修改日期和时间。
// 创建可变的日期时间
MutableDateTime mutableDateTime new MutableDateTime();
mutableDateTime.addDays(7);3. ThreeTen-Extra
3.1 Interval
Interval 类在 ThreeTen-Extra 中也存在用于表示时间间隔。
Interval interval Interval.of(LocalDateTime.now(), LocalDateTime.now().plusHours(2));3.2 YearQuarter
YearQuarter 表示年份的季度。
YearQuarter yearQuarter YearQuarter.now();
int year yearQuarter.getYear();
int quarter yearQuarter.getQuarter();3.3 YearWeek
YearWeek 表示年份的周。
YearWeek yearWeek YearWeek.now();
int year yearWeek.getYear();
int week yearWeek.getWeek();3.4 与时间相关的工具
ThreeTen-Extra 提供了许多与时间相关的工具方法如 YearMonth、DayOfWeekUtil 等。
YearMonth yearMonth YearMonth.now();
DayOfWeekUtil.isWeekend(DayOfWeek.SATURDAY);3.5 YearQuarterRange
YearQuarterRange 类表示两个YearQuarter之间的范围方便进行季度的区间操作。
YearQuarter startQuarter YearQuarter.of(2022, 2);
YearQuarter endQuarter YearQuarter.of(2023, 1);
YearQuarterRange quarterRange YearQuarterRange.of(startQuarter, endQuarter);3.6 YearQuarterAdjusters
YearQuarterAdjusters 提供了一系列用于调整YearQuarter的工具方法例如获取下一个季度的调整器。
YearQuarter nextQuarter YearQuarter.now().with(YearQuarterAdjusters.next());3.7 YearQuarterIterator
YearQuarterIterator 可以用于迭代某个范围内的所有YearQuarter。
YearQuarter startQuarter YearQuarter.of(2022, 2);
YearQuarter endQuarter YearQuarter.of(2023, 1);
YearQuarterIterator iterator YearQuarterIterator.of(startQuarter, endQuarter);
while (iterator.hasNext()) {YearQuarter currentQuarter iterator.next();// 处理每个季度
}3.8 YearQuarterRangeSet
YearQuarterRangeSet 是YearQuarterRange的集合用于处理多个季度范围。
YearQuarterRangeSet rangeSet new YearQuarterRangeSet();
rangeSet.add(YearQuarterRange.of(2022, 2, 2023, 1));
rangeSet.add(YearQuarterRange.of(2023, 2, 2024, 1));boolean contains rangeSet.contains(YearQuarter.of(2022, 3));3.9 YearQuarterSerializer
YearQuarterSerializer 提供了对YearQuarter对象进行序列化和反序列化的功能。
YearQuarter yearQuarter YearQuarter.of(2022, 2);
String serialized YearQuarterSerializer.serialize(yearQuarter);
YearQuarter deserialized YearQuarterSerializer.deserialize(serialized);3.10 YearQuarterParser
YearQuarterParser 用于将字符串解析为YearQuarter对象。
String quarterString 2023-Q2;
YearQuarter parsedQuarter YearQuarterParser.parse(quarterString);ThreeTen-Extra 提供了这些额外的工具和功能增强了对季度的处理能力使得在处理年份和季度相关任务时更加便捷。
4. Apache Commons Lang
4.1 DateUtils
DateUtils 提供了日期操作的各种实用方法。
Date date DateUtils.addDays(new Date(), 7);4.2 DurationFormatUtils
DurationFormatUtils 用于格式化持续时间。
String formattedDuration DurationFormatUtils.formatDurationHMS(3600000);4.3 StopWatch
StopWatch 用于测量时间间隔。
StopWatch stopwatch new StopWatch();
stopwatch.start();
// Perform some operation
stopwatch.stop();
long elapsedMillis stopwatch.getTime();4.4 FastDateFormat
FastDateFormat 提供了一种快速、线程安全的日期格式化方式。
String formattedDate FastDateFormat.getInstance(yyyy-MM-dd).format(new Date());4.5 DateUtils解析字符串为日期
DateUtils 不仅提供了日期操作的方法还支持将字符串解析为日期对象。
String dateString 2023-05-15;
Date parsedDate DateUtils.parseDate(dateString, yyyy-MM-dd);4.6 DurationFormatUtils格式化毫秒为友好格式
DurationFormatUtils 可以将毫秒数格式化为更加友好的时间显示格式例如“2小时30分钟”。
long durationMillis 7380000; // 2小时30分钟
String formattedDuration DurationFormatUtils.formatDurationWords(durationMillis, true, true);4.7 StopWatch记录多个阶段的时间
StopWatch 不仅用于测量整个时间间隔还支持记录多个阶段的时间。
StopWatch stopwatch new StopWatch();stopwatch.start();
// Perform some operation
stopwatch.split(); // 记录第一个阶段的时间// Perform another operation
stopwatch.unsplit(); // 撤销对第一个阶段的记录stopwatch.split(); // 记录第二个阶段的时间
stopwatch.stop();long firstSegmentMillis stopwatch.getSplitTime(); // 获取第一个阶段的时间
long secondSegmentMillis stopwatch.getSplitTime(); // 获取第二个阶段的时间4.8 FastDateFormat解析字符串为日期
FastDateFormat 不仅用于日期格式化还可以将字符串解析为日期对象。
String dateString 2023-05-15;
Date parsedDate FastDateFormat.getInstance(yyyy-MM-dd).parse(dateString);4.9 StopWatch获取总时间和各阶段时间
StopWatch 提供了获取总时间和各阶段时间的方法方便在程序中记录和输出详细的时间信息。
StopWatch stopwatch new StopWatch();stopwatch.start();
// Perform some operation
stopwatch.stop();long totalMillis stopwatch.getTime(); // 获取总时间
long firstSegmentMillis stopwatch.getSplitTime(); // 获取第一个阶段的时间
long secondSegmentMillis stopwatch.getSplitTime(); // 获取第二个阶段的时间Apache Commons Lang 的这些工具不仅提供了基础的日期和时间操作还为时间测量和格式化提供了便捷的工具使得时间处理更加灵活和方便。
5. ChronoUnit
5.1 表示时间单位的枚举
ChronoUnit 是 Java 8 中引入的枚举表示不同的时间单位。
long daysBetween ChronoUnit.DAYS.between(LocalDate.now(), LocalDate.now().plusDays(7));5.2 用于日期和时间计算的工具
ChronoUnit 提供了许多方法用于日期和时间的计算。
LocalDate tomorrow LocalDate.now().plus(1, ChronoUnit.DAYS);这些库提供了广泛的功能使得在 Java 中处理日期和时间变得更加方便和灵活。
5.3 常见的 ChronoUnit
除了上述示例中的DAYSChronoUnit 还包括其他常见的时间单位如YEARS、MONTHS、HOURS等。
long yearsBetween ChronoUnit.YEARS.between(LocalDate.of(2000, 1, 1), LocalDate.now());
long monthsBetween ChronoUnit.MONTHS.between(LocalDate.of(2000, 1, 1), LocalDate.now());
long hoursBetween ChronoUnit.HOURS.between(LocalDateTime.now().minusHours(5), LocalDateTime.now());5.4 ChronoUnit 的 Plus 和 Minus 操作
ChronoUnit 可以用于日期和时间的加减操作。
LocalDateTime futureDateTime LocalDateTime.now().plus(1, ChronoUnit.MONTHS);
LocalDateTime pastDateTime LocalDateTime.now().minus(2, ChronoUnit.YEARS);5.5 使用 ChronoUnit 进行时间戳的计算
ChronoUnit 可以用于计算两个时间戳之间的差距。
Instant earlierInstant Instant.now().minus(1, ChronoUnit.HOURS);
Instant laterInstant Instant.now();long secondsBetween ChronoUnit.SECONDS.between(earlierInstant, laterInstant);5.6 通过 ChronoUnit 计算日期调整
ChronoUnit 还可以用于进行日期调整例如将日期调整到下一个工作日。
LocalDate nextWorkday LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.MONDAY));
long daysUntilNextWorkday ChronoUnit.DAYS.between(LocalDate.now(), nextWorkday);使用ChronoUnit进行日期和时间的计算和调整使得处理不同时间单位的操作更为直观和便捷。
总结
通过学习这些日期与时间处理库读者将能够更好地处理项目中的时间相关任务。我们将深入了解每个库的使用方法提供详细的代码示例以便读者能够轻松上手。从简单的日期获取到复杂的时间间隔计算这篇文章将为读者提供全面的指南。