)
1. java8 - Instant重要知识点定义Instant表示时间线上的一个瞬时点精确到纳秒主要用于处理机器可读的时间戳基于 Unix 时间戳从 1970 年 1 月 1 日 00:00:00 UTC 开始的秒数。常用方法now()获取当前的瞬时点。plusSeconds(long secondsToAdd)在当前瞬时点上增加指定的秒数。minusSeconds(long secondsToSubtract)在当前瞬时点上减去指定的秒数。isBefore(Instant otherInstant)和isAfter(Instant otherInstant)用于比较两个Instant的先后顺序。代码示例importjava.time.Instant;publicclassOrderService{publicvoidrecordOrderTime(){InstantorderTimeInstant.now();System.out.println(订单创建时间: orderTime);// 模拟订单处理 10 秒InstantprocessedTimeorderTime.plusSeconds(10);System.out.println(订单处理完成时间: processedTime);}}2. java8 - LocalDate重要知识点定义LocalDate表示一个不包含时间和时区的日期例如 2025 - 02 - 24。创建方式now()获取当前日期。of(int year, int month, int dayOfMonth)根据指定的年、月、日创建日期。代码示例importjava.time.LocalDate;publicclassProductService{publicvoidcheckProductExpiry(){LocalDatecurrentDateLocalDate.now();LocalDateexpiryDateLocalDate.of(2025,12,31);if(currentDate.isAfter(expiryDate)){System.out.println(商品已过期);}else{System.out.println(商品未过期);}}}3. java8 - LocalDate 日期加减重要知识点方法plusDays(long daysToAdd)增加指定的天数。plusWeeks(long weeksToAdd)增加指定的周数。plusMonths(long monthsToAdd)增加指定的月数。plusYears(long yearsToAdd)增加指定的年数。minusDays(long daysToSubtract)、minusWeeks(long weeksToSubtract)等用于减去相应的时间单位。代码示例importjava.time.LocalDate;publicclassPromotionService{publicvoidcalculatePromotionEndDate(){LocalDatestartDateLocalDate.now();LocalDateendDatestartDate.plusDays(7);System.out.println(促销开始日期: startDate);System.out.println(促销结束日期: endDate);}}4. java8 - LocalDate 日期加减方法续重要知识点可以使用plus(TemporalAmount amountToAdd)和minus(TemporalAmount amountToSubtract)方法进行更灵活的日期加减操作TemporalAmount可以是Period等。代码示例importjava.time.LocalDate;importjava.time.Period;publicclassDeliveryService{publicvoidestimateDeliveryDate(){LocalDateorderDateLocalDate.now();PerioddeliveryPeriodPeriod.ofDays(3);LocalDateestimatedDeliveryDateorderDate.plus(deliveryPeriod);System.out.println(订单日期: orderDate);System.out.println(预计送达日期: estimatedDeliveryDate);}}5. java8 - LocalDate 比较日期方法重要知识点方法isBefore(LocalDate other)判断当前日期是否在指定日期之前。isAfter(LocalDate other)判断当前日期是否在指定日期之后。isEqual(LocalDate other)判断两个日期是否相等。代码示例importjava.time.LocalDate;publicclassInvoiceService{publicvoidcheckInvoiceDueDate(){LocalDatecurrentDateLocalDate.now();LocalDatedueDateLocalDate.of(2025,3,1);if(currentDate.isAfter(dueDate)){System.out.println(发票已逾期);}elseif(currentDate.isBefore(dueDate)){System.out.println(发票未逾期);}else{System.out.println(发票今日到期);}}}6. java8 - LocalDate 修改年月日重要知识点方法withYear(int year)修改年份。withMonth(int month)修改月份。withDayOfMonth(int dayOfMonth)修改日期。代码示例importjava.time.LocalDate;publicclassInventoryService{publicvoidadjustInventoryUpdateDate(){LocalDateoriginalDateLocalDate.now();LocalDateadjustedDateoriginalDate.withYear(2026).withMonth(6).withDayOfMonth(15);System.out.println(原库存更新日期: originalDate);System.out.println(调整后的库存更新日期: adjustedDate);}}7. java8 - LocalDate 使用总结重要知识点LocalDate适用于处理只涉及日期的场景如生日、到期日期、促销日期等。它提供了丰富的日期操作方法包括日期的创建、加减、比较和修改等使用起来非常方便。8. java8 - LocalTime - 1重要知识点定义LocalTime表示一个不包含日期和时区的时间例如 10:30:00。创建方式now()获取当前时间。of(int hour, int minute)、of(int hour, int minute, int second)等根据指定的时、分、秒创建时间。代码示例importjava.time.LocalTime;publicclassCustomerService{publicvoidcheckServiceHours(){LocalTimecurrentTimeLocalTime.now();LocalTimestartTimeLocalTime.of(9,0);LocalTimeendTimeLocalTime.of(18,0);if(currentTime.isAfter(startTime)currentTime.isBefore(endTime)){System.out.println(客服服务时间内);}else{System.out.println(客服休息时间);}}}9. java8 - LocalTime - 2重要知识点时间操作方法plusHours(long hoursToAdd)、plusMinutes(long minutesToAdd)等增加相应的时间单位。minusHours(long hoursToSubtract)、minusMinutes(long minutesToSubtract)等减去相应的时间单位。isBefore(LocalTime other)和isAfter(LocalTime other)比较时间先后。代码示例importjava.time.LocalTime;publicclassFlashSaleService{publicvoidcheckFlashSaleTime(){LocalTimecurrentTimeLocalTime.now();LocalTimesaleStartTimeLocalTime.of(12,0);LocalTimesaleEndTimesaleStartTime.plusHours(1);if(currentTime.isAfter(saleStartTime)currentTime.isBefore(saleEndTime)){System.out.println(限时抢购进行中);}else{System.out.println(限时抢购未开始或已结束);}}}10. java8 - LocalDateTime重要知识点定义LocalDateTime表示一个既包含日期又包含时间的对象不包含时区信息例如 2025 - 02 - 24T10:30:00。创建方式now()获取当前的日期和时间。of(LocalDate date, LocalTime time)根据LocalDate和LocalTime创建LocalDateTime。of(int year, int month, int dayOfMonth, int hour, int minute)等直接指定年、月、日、时、分等信息创建。代码示例importjava.time.LocalDate;importjava.time.LocalDateTime;importjava.time.LocalTime;publicclassOrderProcessingService{publicvoidprocessOrder(){LocalDateorderDateLocalDate.of(2025,2,24);LocalTimeorderTimeLocalTime.of(14,30);LocalDateTimeorderDateTimeLocalDateTime.of(orderDate,orderTime);System.out.println(订单创建时间: orderDateTime);}}11. java8 - LocalDateTime 总结重要知识点LocalDateTime结合了LocalDate和LocalTime的功能适用于需要同时处理日期和时间的场景如订单创建时间、会议安排等。它继承了LocalDate和LocalTime的大部分操作方法方便进行日期和时间的组合操作。12. java8 - MonthDay重要知识点定义MonthDay表示一年中的某个月和日不包含年份信息例如 --02 - 24。用途常用于表示每年重复的日期如生日、纪念日等。创建方式now()获取当前的月和日。of(int month, int dayOfMonth)根据指定的月和日创建MonthDay。代码示例importjava.time.MonthDay;publicclassCustomerBirthdayService{publicvoidcheckCustomerBirthday(){MonthDaycurrentMonthDayMonthDay.now();MonthDaycustomerBirthdayMonthDay.of(2,24);if(currentMonthDay.equals(customerBirthday)){System.out.println(今天是客户的生日发送生日祝福和优惠活动);}}}13. java8 - YearMonth重要知识点定义YearMonth表示一年中的某个月包含年份和月份信息例如 2025 - 02。用途常用于处理与月份相关的统计、报表等业务如每月销售额统计。创建方式now()获取当前的年和月。of(int year, int month)根据指定的年和月创建YearMonth。代码示例importjava.time.YearMonth;publicclassMonthlySalesService{publicvoidcalculateMonthlySales(){YearMonthcurrentYearMonthYearMonth.now();System.out.println(当前统计月份: currentYearMonth);// 模拟计算该月销售额的逻辑System.out.println(该月销售额计算完成);}}14. java8 - Period重要知识点定义Period用于表示两个日期之间的时间间隔以年、月、日为单位。创建方式between(LocalDate startDate, LocalDate endDate)计算两个LocalDate之间的时间间隔。常用方法getYears()、getMonths()、getDays()分别获取时间间隔中的年、月、日数。代码示例importjava.time.LocalDate;importjava.time.Period;publicclassOrderDurationService{publicvoidcalculateOrderDuration(){LocalDateorderDateLocalDate.of(2025,1,1);LocalDatedeliveryDateLocalDate.of(2025,1,10);PerioddurationPeriod.between(orderDate,deliveryDate);System.out.println(订单从下单到送达历时: duration.getDays() 天);}}15. java8 - Period 使用总结重要知识点Period主要用于处理日期之间的间隔以年、月、日为单位进行计算。在处理与日期跨度相关的业务场景时非常有用如计算订单处理周期、会员有效期等。16. java8 - Duration重要知识点定义Duration用于表示两个时间点之间的时间间隔以秒和纳秒为单位适用于处理更精确的时间间隔如程序执行时间、视频播放时长等。创建方式between(Temporal startInclusive, Temporal endExclusive)计算两个Temporal对象如LocalDateTime、Instant等之间的时间间隔。常用方法getSeconds()获取时间间隔的秒数。toMinutes()、toHours()、toDays()将时间间隔转换为分钟、小时、天。代码示例importjava.time.Duration;importjava.time.LocalDateTime;publicclassPaymentProcessingService{publicvoidmeasurePaymentProcessingTime(){LocalDateTimepaymentStartTimeLocalDateTime.now();// 模拟支付处理逻辑try{Thread.sleep(2000);}catch(InterruptedExceptione){e.printStackTrace();}LocalDateTimepaymentEndTimeLocalDateTime.now();DurationprocessingDurationDuration.between(paymentStartTime,paymentEndTime);System.out.println(支付处理耗时: processingDuration.getSeconds() 秒);}}17. java8 - Duration 其他用法重要知识点Duration还可以进行时间间隔的加减操作例如plus(Duration other)、minus(Duration other)等。代码示例importjava.time.Duration;importjava.time.LocalDateTime;publicclassDeliveryEstimationService{publicvoidadjustDeliveryEstimation(){LocalDateTimeestimatedDeliveryTimeLocalDateTime.of(2025,2,25,10,0);DurationdelayDuration.ofHours(2);LocalDateTimenewEstimatedDeliveryTimeestimatedDeliveryTime.plus(delay);System.out.println(原预计送达时间: estimatedDeliveryTime);System.out.println(调整后的预计送达时间: newEstimatedDeliveryTime);}}18. java8 - DateTimeFormatter重要知识点定义DateTimeFormatter用于格式化和解析日期时间对象将日期时间对象转换为字符串或将字符串转换为日期时间对象。创建方式ofPattern(String pattern)根据指定的模式创建DateTimeFormatter。提供了一些预定义的格式化器如DateTimeFormatter.ISO_LOCAL_DATE、DateTimeFormatter.ISO_LOCAL_TIME等。常用方法format(TemporalAccessor temporal)将日期时间对象格式化为字符串。parse(CharSequence text, TemporalQueryT query)将字符串解析为日期时间对象。代码示例importjava.time.LocalDate;importjava.time.format.DateTimeFormatter;publicclassOrderDateDisplayService{publicvoiddisplayOrderDate(){LocalDateorderDateLocalDate.of(2025,2,24);DateTimeFormatterformatterDateTimeFormatter.ofPattern(yyyy-MM-dd);StringformattedDateorderDate.format(formatter);System.out.println(订单日期: formattedDate);StringdateString2025-03-15;LocalDateparsedDateLocalDate.parse(dateString,formatter);System.out.println(解析后的日期: parsedDate);}}