java 已经出到 17 了,而小编还在用 8 的版本,在 8 中已经推出了新的日期 API,不在使用 。java.time 包下提供了用于日期、时间、实例和周期的主要 API。
获取今天日期 {#获取今天日期}
LocalDate 存储了日期,如:2022-01-09,它不包含时间。
|-----------------|------------------------------------------------------------------------------------------------------|
| 1 2 3 4
| public static void main(String[] args) { LocalDate now = LocalDate.now(); System.out.println(now); } |
设置日期 {#设置日期}
of 方法是一个静态方法,接收年、月、日三个 int 参数,返回 LocalDate 对象。需要注意的是月不是从 0 开始的。
|-----------------|---------------------------------------------------------------------------------------------------------------|
| 1 2 3 4
| public static void main(String[] args) { LocalDate date = LocalDate.of(2022,1,9); System.out.println(date); } |
日期判断 {#日期判断}
日期操作第一个会想到的就是两个日期是否相等的操作
|---------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9
| public static void main(String[] args) { LocalDate date = LocalDate.of(2022,1,10); LocalDate today = LocalDate.now(); if(date.equals(today)) { System.out.println("日期相等"); } else { System.out.println("日期不相等"); } } |
当前日期是否早于、晚于另外的日期,使用 isAfter() 和 isBefore() 方法。
|------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalDate date = LocalDate.of(2022,1,9); if(today.isAfter(date)) { System.out.println("今天已经晚了"); } else { System.out.println("今天还早"); } if(today.isBefore(date)) { System.out.println("今天在前面"); } else { System.out.println("今天在后面"); } } |
匹配周期性的日期 {#匹配周期性的日期}
在某些业务中,每年、每月、每周都会有一个特定的日期,比如每个月的账单日、每年的生日等日期,这时就不能把年、月等放入日期比较。可以使用 DayOfWeek、MonthDay、YearMonth 类处理这类日期。
|------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10
| public static void main(String[] args) { LocalDate date = LocalDate.of(2022,1,9); MonthDay monthDay = MonthDay.of(date.getMonth(), date.getDayOfMonth()); MonthDay today = MonthDay.from(LocalDate.now()); if(today.equals(monthDay)) { System.out.println("今天已经到特定的日期了"); } else { System.out.println("今天还没有到特定的日期"); } } |
一周、月、年前、后的日期 {#一周月年前后的日期}
ChronoUnit 是用来表示时间的单位,比如:ChronoUnit.YEARS 年,ChronoUnit.WEEKS 周。minus()、plus() 方法用来减去、加上一个时间周期。
|------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalDate preWeek = today.minus(1, ChronoUnit.WEEKS); System.out.println(preWeek); LocalDate afterWeek = today.plus(1, ChronoUnit.WEEKS); System.out.println(afterWeek); LocalDate preMon = today.minus(1, ChronoUnit.MONTHS); System.out.println(preMon); LocalDate afterMon = today.plus(1, ChronoUnit.MONTHS); System.out.println(afterMon); LocalDate preYear = today.minus(1, ChronoUnit.YEARS); System.out.println(preYear); LocalDate afterYear = today.plus(1, ChronoUnit.YEARS); System.out.println(afterYear); } |
日期间隔 {#日期间隔}
计算两个日期的间隔了多少天、月是一个很常规的业务操作。月是用 Period 类计算,天使用 LocalDate 的 toEpochDay() 方法计算。
|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8
| public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalDate date = LocalDate.of(2022,4,10); Period period = Period.between( date,today); System.out.println("间隔了" + period.getMonths() + "月"); System.out.println("间隔了" + (today.toEpochDay() - date.toEpochDay()) + "天"); } |
时间戳 {#时间戳}
Instant 类有一个静态工厂方法now()会返回当前的时间戳。
|-------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5
| public static void main(String[] args) { Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.toEpochMilli()); } |
格式化日期 {#格式化日期}
DateTimeFormatter 是一个线程安全的日期和时间格式化器,下面是用 DateTimeFormatter.BASIC_ISO_DATE 将 20210109 格式化成了 2021-01-09。
|-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5
| public static void main(String[] args) { String str = "20210109"; LocalDate formatted = LocalDate.parse(str, DateTimeFormatter.BASIC_ISO_DATE); System.out.println("格式化后的日期为: "+ formatted); } |
DateTimeFormatter 也可以自定义格式化字符串。
|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6
| public static void main(String[] args) { String str = "09012021"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy"); LocalDate date = LocalDate.parse(str, formatter); System.out.print(date); } |
是否为闰年 {#是否为闰年}
小编在学程序之初被闰年折磨的死去活来,给个年份计算是不是闰年。java 8 的 isLeapYear() 就很好的解决了这个问题。
|---------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9
| public static void main(String[] args) { LocalDate today = LocalDate.now(); if(today.isLeapYear()){ System.out.println("闰年"); }else { System.out.println("不是闰年"); } } |
日期转字符串 {#日期转字符串}
上面格式化的例子是将字符串转为日期,下面是将日期转为字符串。
|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6
| public static void main(String[] args) { LocalDate now = LocalDate.now(); DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String str = now.format(format); System.out.print(str); } |