JDK1.8日期类常用方法总结

发布时间 2023-07-12 17:57:49作者: 海哥哥99

Java 1.8推荐使用java.time包中的日期和时间类来处理日期和时间。这些类提供了更简单、更清晰、更易于使用的API。

在日常工作中最常用的三个时间处理API:
     1、LocalDate 只显示日期部分(yyyy-MM-dd)。
     2、LocalTime 只显示时间部分(HH:mm:ss)。
     3、LocalDateTime:表示既有日期又有时间的日期时间,就是将上面两个结合起来。

当然还提供了其他API如 :ZonedDateTime:表示带有时区的日期时间;Duration:表示时间间隔,例如两个时间点之间的差异(返回相差的毫秒数)。Period:表示日期间隔,例如两个日期之间的差异(返回相差的天数)。

功能1:获取当前时间,以及获取年月日、星期等方法

LocalDateTime.now(); 获取到的时间样式:2023-07-12T14:44:08.644
 
LocalDate.now(); 获取到的时间样式:2023-07-12
 
LocalTime.now(); 获取到的时间样式:14:44:15.105
 
 
getYear();获取年
getMinute();获取分钟
getHour();获取小时
getDayOfMonth 获得月份天数(1-31)
getDayOfYear 获得年份天数(1-366)
getDayOfWeek 获得星期几(返回一个 DayOfWeek枚举值)
getMonth 获得月份, 返回一个 Month 枚举值
getMonthValue 获得月份(1-12)
getYear 获得年份

功能2:将当前时间或者指定时间转化为指定格式的字符串

 
String dateTime = "2023-01-01 12:20:20";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(dateTime, dateTimeFormatter);
 
String date = "2023-01-01";
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse(date, dateTimeFormatter2);
 
String time = "12:20:20";
DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.parse(time, dateTimeFormatter3);

打印结果:
localDateTime 2023-01-01T12:20:20
localDate  2023-01-01
localTime  12:20:20

功能3:获取一个时间的前/后 一月,一天,一小时,一分钟的时间

//获取当前时间前一天的时间
 LocalDateTime minusDays = LocalDateTime.now().minusDays(1);
  ...
 //获取当前时间后一天的时间
 LocalDateTime dateTime = LocalDateTime.now().plusDays(1);
 ...

minus...表示减去,plus表示增加。

功能4:获取当天/指定日期的开始时间,结束时间

平时在做统计查询时经常会用到这个功能,比如开始时间,结束时间都选择当天,他查询的是当前的0点到24点的数据,业务就需要获取到选择的开始日期的0点,结束日期的23:59:59秒。

//获取日期的开始时间
 LocalDateTime startTime = localDateTime.with(LocalTime.MIN);
 //获取日期的结束时间
 LocalDateTime endTime = localDateTime.with(LocalTime.MAX);

startTime 打印结果:2023-07-12T00:00
endTime 打印结果: 2023-07-12T23:59:59.999999999

源码:

/**
 * The minimum supported {@code LocalTime}, '00:00'.
 * This is the time of midnight at the start of the day.
 */
public static final LocalTime MIN;
/**
 * The maximum supported {@code LocalTime}, '23:59:59.999999999'.
 * This is the time just before midnight at the end of the day.
 */
public static final LocalTime MAX;
/**
 * The time of midnight at the start of the day, '00:00'.
 */
public static final LocalTime MIDNIGHT;
/**
 * The time of noon in the middle of the day, '12:00'.
 */
public static final LocalTime NOON;
功能5:两个日期判断大小,是否相等。
//时间1
LocalDateTime time1 = LocalDateTime.now();
//时间2
LocalDateTime time2 = LocalDateTime.of(2023, 8, 8, 12, 24, 40);
//时间1 是否 在时间2之前
boolean before = time1.isBefore(time2);
//时间1 是否 在时间2之后
boolean after = time1.isAfter(time2);
//时间1 与 在时间2是否相等
boolean timeEqual = time1.isEqual(time2);

功能6:其他一些获取特定时间的实例

//取本月第1天
LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth());
 
//获取本月最后一天
LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth());
 
//取当前时间月份的第一个周一
LocalDateTime.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
...

TemporalAdjusters API 提供了很多时间的计算方法,例如前、后一个月、年的第一天,最后一天 等等,需要的可以研究一下。

功能7:LocalTime 操作时间的方法

LocalTime.now();//取当前时间,默认带毫秒
LocalTime.now().withNano(0);//去除毫秒
LocalTime.now().plusHours(2);//2小时后的时间
LocalTime.now().minusHours(2);//2小时前的时间
LocalTime.now().plusMinutes(30);//30分钟后的时间
...

LocalDateTime 与 LocalTime 操作差不多,只是可以带日期和时间