Java8中日期的特性,别再用Date来处理日期啦~

01. 获取当前日期

格式:yyyy-MM-dd

LocalDate today = LocalDate.now();
System.out.println("今天的日期:" + today);

02. 获取日期中的年/月/日

getMonthValue() 获取月

getDayOfMonth() 获取天

LocalDate today = LocalDate.now();
int year = today.getYear();
System.out.println("年:" + year);

03. 创建自定义日期

LocalDate date = LocalDate.of(2060, 02, 17);
System.out.println("自定义时间:" + date);

04. 判断两个日期是否相等

LocalDate today = LocalDate.now();
LocalDate demoDate = LocalDate.of(2020, 2, 25);
if (today.equals(demoDate)) {
    System.out.println("两个时间相等!");
} else {
    System.out.println("两个时间不相等!");
}

05. 判断日期周期性 例如:国庆节

LocalDate national = LocalDate.of(2020, 10, 1);
MonthDay monthDay = MonthDay.of(national.getMonth(), national.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(today);
if(currentMonthDay.equals(monthDay)){
    System.out.println("国庆节到啦,举国同庆!");
}else{
    System.out.println("国庆节还没到!");
}

06. 获取当前时间

LocalTime time = LocalTime.now();
System.out.println("当前时间:" + time);

07. 获取指定N小时/N分/N秒后的时间

plusMinutes(9) 9分钟后

plusSeconds(9) 9秒钟后

LocalTime time = LocalTime.now();
LocalTime plusHours = time.plusHours(9);
System.out.println("9小时后:" + plusHours);

08. 计算N天/N周/N月/N年后的日期 ChronoUnit声明单位

LocalDate today = LocalDate.now();
LocalDate plusYears = today.plus(2, ChronoUnit.YEARS);
System.out.println("两年后:" + plusYears);

09. 计算N天/N周/N月/N年前的日期 ChronoUnit声明单位

LocalDate today = LocalDate.now();
LocalDate minusYears = today.minus(2, ChronoUnit.YEARS);
System.out.println("两年前:" + minusYears);

10. Clock时钟类,获取当前时间戳

Clock clock = Clock.systemUTC();
System.out.println("当前时间戳:" + clock.millis());
Clock systemDefaultClock = Clock.systemDefaultZone();
System.out.println("当前默认时区时间戳:" + systemDefaultClock.millis());

11. 判断某个日期小于或大于指定日期 isAfter/isBefore

LocalDate today = LocalDate.now();
if(today.isAfter(future)){
    System.out.println("在指定日期之前");
}else{
    System.out.println("在指定日期之后");
}

12. 时区处理

ZoneId americaZone = ZoneId.of("America/New_York");
LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime americaZoneTime = ZonedDateTime.of(localDateTime, americaZone);
System.out.println("当前时间的美国纽约时间:" + americaZoneTime);

13. 获取当前年月/当前月份多少天

YearMonth currentYearMonth = YearMonth.now();
System.out.println("当前年月:" + currentYearMonth);
System.out.println("当前月份有多少天:" + currentYearMonth.lengthOfMonth());
YearMonth futureYearMonth = YearMonth.of(2021, Month.FEBRUARY);
System.out.println("指定年月:" + futureYearMonth);

14. 校验日期是否是闰年

isLeapYear()函数,闰年返回true,否则返回false

if(today.isLeapYear()){
    System.out.println(today.getYear() + "是闰年!");
}else {
    System.out.println(today.getYear() + "不是闰年!");
}

15. 计算两个时间之间的天数/月数

LocalDate today = LocalDate.now();
LocalDate futureLocalDate = LocalDate.of(2020, 3, 25);
Period period = Period.between(today, futureLocalDate);
System.out.println("两个日期相差:" + period.getMonths() + "月!");

16. 获取当前时间戳

Instant instant = Instant.now();
System.out.println("当前时间戳:" + instant.toEpochMilli());

17. 格式化日期

String strDate = "20220101";
LocalDate parseLocalDate = LocalDate.parse(strDate, DateTimeFormatter.BASIC_ISO_DATE);
System.out.println("格式化后的日期:" + parseLocalDate);

18. 字符串日期互转

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String strNow = now.format(format);
System.out.println("日期转字符串:" + strNow);
System.out.println("字符串转日期:" + LocalDate.parse(strNow, format));

文章发布自:笑凡尘,转载请注明出处,谢谢!