Java 8 LocalDateTime日期的介绍与使用【附DateUtils日期处理工具完整代码】
About me
一个爱学习、爱分享、爱交流的程序员Boy;
欢迎关注个人微信公众号【Java烂笔头】,微信小程序【Java烂笔头】,一起交流、共同进步;
文章中源码获取:
前言
最近开发项目遇到很多日期处理相关的问题,比如本周的结束时间、上个月的开始时间、本季度的开始时间、指定日期的时间间隔……等等
开始还在用Calendar……那叫一个麻烦
后来发现LocalDateTime!真香!
简介
日期工具在Java开发过程中还是很常用的,尤其是在统计计算过程中,掌握好日期计算会大大提升开发效率!
Java 8 新特性之一:加强对时间和日期的处理,明确了日期时间概念;
例如:瞬时(instant)、时长(duration)、日期、时间、时区和周期。同时继承了Joda 库按人类语言和计算机各自解析的时间处理方式。不同于老版本,新API基于ISO标准日历系统,java.time包下的所有类都是不可变类型而且线程安全。
新增的关键日期类主要有五种:
java.time.Instant->瞬时时间
默认格式:yyyy-MM-ddTHH:mm:ss.SSSSSSSSSZ,例如:2022-10-15T13:21:48.602Z
/**
*最小支持瞬时时间
* The minimum supported {@code Instant}, '-1000000000-01-01T00:00Z'.
* This could be used by an application as a "far past" instant.
*/
public static final Instant MIN = Instant.ofEpochSecond(MIN_SECOND, 0);
/**
*最大支持瞬时时间
* The maximum supported {@code Instant}, '1000000000-12-31T23:59:59.999999999Z'.
* This could be used by an application as a "far future" instant.
*/
public static final Instant MAX = Instant.ofEpochSecond(MAX_SECOND, 999_999_999);
java.time.LocalDate ->只对年月日做出处理
默认格式:yyyy-MM-dd, 例如:2022-10-15
/**
* 最小支持日期
* The minimum supported {@code LocalDate}, '-999999999-01-01'.
* This could be used by an application as a "far past" date.
*/
public static final LocalDate MIN = LocalDate.of(Year.MIN_VALUE, 1, 1);
/**
* 最大支持日期
* The maximum supported {@code LocalDate}, '+999999999-12-31'.
* This could be used by an application as a "far future" date.
*/
public static final LocalDate MAX = LocalDate.of(Year.MAX_VALUE, 12, 31);
java.time.LocalTime ->只对时分秒纳秒做出处理
默认格式:HH:mm:ss.SSSSSSSSS 例如:21:30:39.412
/**
* 最小支持时间
* 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;
java.time.LocalDateTime ->同时可以处理年月日和时分秒
默认格式:yyyy-MM-ddTHH:mm:ss.SSSSSSSSS 例如:2022-10-15T21:30:39.413
/**
* 最小支持日期时间
* The minimum supported {@code LocalDateTime}, '-999999999-01-01T00:00:00'.
* This is the local date-time of midnight at the start of the minimum date.
*/
public static final LocalDateTime MIN = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN);
/**
* 最大支持日期时间
* The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'.
* This is the local date-time just before midnight at the end of the maximum date.
*/
public static final LocalDateTime MAX = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX);
java.time.ZonedDateTime:最完整的日期-时间-时区-相对UTC或格林威治的时差
默认格式:yyyy-MM-ddTHH:mm:ss.SSSSSSSSS+08:00[Asia/Shanghai] 例如:2022-10-15T21:30:39.414+08:00[Asia/Shanghai]
一、创建LocalDateTime
方法说明:
/**
* static LocalDateTime ofInstant(Instant instant, ZoneId zone) 通过Instant实例与时区创建时间
* static LocalDateTime now() 获取默认时区的当前日期时间,默认格式yyyy-MM-ddTHH:mm:ss.SSSSSSSSS
* static LocalDateTime now(ZoneId zone) 从指定时区获取日期时间
* static LocalDateTime now(Clock clock) 从指定闹钟获取日期时间
* static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) 根据年月日时分秒创建日期时间
* static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) 根据年月日时分秒纳秒创建日期时间
* static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) 根据年月日时分秒创建日期时间
* static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) 根据年月日时分秒纳秒创建日期时间
* static LocalDateTime of(LocalDate date, LocalTime time) 根据LocalDate与LocalTime创建时间
* static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) 通过毫秒数、纳秒数及时区创建时间epochSecond:从1970-01-01T00:00:00到指定时间的秒数
*/
使用示例:
System.out.println("创建Instant实例时间:" + LocalDateTime.ofInstant(Instant.now(), ZoneId.of("UTC")));
System.out.println("当前默认日期时间:" + LocalDateTime.now());
System.out.println("当前指定时区时间:" + LocalDateTime.now(ZoneId.systemDefault()));
System.out.println("当前指定闹钟时间:" + LocalDateTime.now(Clock.systemUTC()));
System.out.println("指定年月日时分秒时间:" + LocalDateTime.of(2022, Month.OCTOBER, 15, 9, 51, 15));
System.out.println("指定时分秒纳秒时间:" + LocalDateTime.of(2022, Month.OCTOBER, 15, 9, 51, 15, 1));
System.out.println("指定时分秒时间:" + LocalDateTime.of(2022, 10, 15, 9, 51, 15));
System.out.println("指定时分秒纳秒时间:" + LocalDateTime.of(2022, 12, 15, 9, 51, 15, 1));
System.out.println("localDate、LocalTime时间:" + LocalDateTime.of(LocalDate.now(), LocalTime.now()));
System.out.println("通过秒、毫秒创建时间:" + LocalDateTime.ofEpochSecond(Instant.now().getEpochSecond(), 222, ZoneOffset.UTC));
测试结果:
创建Instant实例时间:2022-10-15T13:56:22.953
当前默认日期时间:2022-10-15T21:56:22.953
当前指定时区时间:2022-10-15T21:56:22.953
当前指定闹钟时间:2022-10-15T13:56:22.954
指定年月日时分秒时间:2022-10-15T09:51:15
指定时分秒纳秒时间:2022-10-15T09:51:15.000000001
指定时分秒时间:2022-10-15T09:51:15
指定时分秒纳秒时间:2022-12-15T09:51:15.000000001
localDate、LocalTime时间:2022-10-15T21:56:22.955
通过秒、毫秒创建时间:2022-10-15T13:56:22.000000222
二、获取年月日时分秒纳秒
方法说明:
/**
* int getYear() 获取年份
* Month getMonth() 使用月枚举类型获取月份
* int getMonthValue() 返回数字月份 1-12月
* int getDayOfMonth() 获取日期在该月是第几天
* DayOfWeek getDayOfWeek() 获取日期是星期几
* int getDayOfYear() 获取日期在该年是第几天
* int getHour() 获取小时, 返回0到23
* int getMinute() 获取分钟, 返回0到59
* int getSecond() 获取秒,返回0到59
* int getNano() 获取纳秒,返回0到999,999,999
*/
使用示例:
//获取当前时间
LocalDateTime now = LocalDateTime.now();
System.out.println("当前默认时间:" + now);
//获取年份
System.out.println("年:" + now.getYear());
//获取月份
System.out.println("月:" + now.getMonth().getValue());
//获取当前日期是当月的第几天
System.out.println("日:" + now.getDayOfMonth());
//获取当前日期是星期几
System.out.println("星期几:" + now.getDayOfWeek());
//获取当前日期是当年的第几天
System.out.println("日:" + now.getDayOfYear());
//获取小时、分钟、秒、纳秒
System.out.println("当前时间:" + now.getHour() + "小时," + now.getMinute() + "分钟