1.1概述
在之前常用类中已经学习到了日期和日历相关的类:Date
、Calendar
类等,
Date date = new Date(); System.out.println("date = " + date);//date = Thu Apr 28 22:37:35 CST 2022 long timeMillis = System.currentTimeMillis();//date1 = Thu Apr 28 22:37:35 CST 2022 Date date1 = new Date(timeMillis); System.out.println("date1 = " + date1); LocalDate nowDate = LocalDate.now(); System.out.println("nowDate = " + nowDate);//nowDate = 2022-04-28 //LocalTime //+ `static LocalTime now()`:获取系统时间 LocalTime localTime = LocalTime.now(); System.out.println("localTime = " + localTime);//localTime1 = 08:32:34
但是以上类在日期处理方面存在一些不太合理的问题:例如月份从0开始,对于日期的输出格式需要借助格式化工具类:计算日期时间之差时,需要转换成毫秒数,进行增减运算等诸多问题。
针对以上的问题,从JDK8开始新增java.time
包,专门用于处理与日期时间相关的数据,对于以上的问题,java8中新增的日期处理类都能简单解决;java中新增的日期相关类主要包含以下:
-
LocalTime
用于表示时间 // -
LocalDate
:用于表示日期 -
LocalDateTime
:用于表示日期时间 -
DateTimeFormatter
:用于日期时间格式化处理
1.2. LocalTime
LocalTime
表示一个具体的时间,是一个不可变的线程安全的实现,时间更精确。该类的构造器是私有的,因此无法直接实例化,需要获取当前系统时间一般直接使用静态方法now()
1.2.1. 常用方法
-
static LocalTime now()
:获取系统时间 -
static LocalTime of(int h,int m,int s)
:根据提供的时分秒获取指定时间 -
long unitl(Temporal endtime,TemporalUnit unit)
:计算当前时间和目标时间endExclusive
之间的差(单位:unit
) -
LocalTime plus(long t,TemporalUnit unit)
:在现有的时间基础上加一个时间(单位:unit
) -
LocalTime minus(long t,TemporalUnit unit)
:在现有的时间基础上减去一个时间(单位:unit
)
1.2.2. 具体使用
//获取当前系统时间
LocalTime time = LocalTime.now();
System.out.println(time);
//根据提供的时间获取LocalTime对象
LocalTime time2 = LocalTime.of(15, 22, 35);
System.out.println(time2);
//计算两个时间之间的差值(单位取决于传入ChronoUnit:小时,分钟,秒)
long exp = time.until(time2, ChronoUnit.SECONDS);
System.out.println(exp);
//在指定的时间基础上增加一个时间值(单位取决于传入ChronoUnit:小时,分钟,秒)
time = time.plus(10,ChronoUnit.MINUTES);
System.out.println(time);
//在指定的时间基础上减去一个时间值(单位取决于传入ChronoUnit:小时,分钟,秒)
time = time.minus(10,ChronoUnit.MINUTES);
System.out.println(time);
1.3. LocalDate
LocalDate
用于表示一个具体的日期对象(年,月,日),是一个不可变的线程安全实现,可以通过其实例获取每个字段信息,需要获取系统日期只需要调用其静态方法now()
;
1.3.1. 常用方法
-
static LocalDate now()
-
static LocalDate of(int year,int month,int day)
-
long unitl(Temporal endExclusive,TemporalUnit unit)
:计算当前日期和目标日期endExclusive
之间的差(单位:unit
) -
LocalTime plus(long amountToAdd,TemporalUnit unit)
:在现有的日期基础上加一个日期(单位:unit
) -
LocalTime minus(long amountToAdd,TemporalUnit unit)
:在现有的日期基础上减去一个日期(单位:unit
)
1.3.2. 具体使用
LocalDate date = LocalDate.now();
System.out.println(date);
//通过给定日期值构建一个LocalDate
LocalDate date2 = LocalDate.of(2023, 9, 15);
System.out.println(date2);
Period until = date.until(date2);
System.out.println("相隔天数:"+until.getDays());
System.out.println("相隔月份:"+until.getMonths());
System.out.println("相隔年数:"+until.getYears());
System.out.println("--------------");
//获取两个日期的差值,单位取决于ChronoUnit(年,月,日)
long days = date.until(date2, ChronoUnit.YEARS);
System.out.println("相隔年数:"+days);
days = date.until(date2, ChronoUnit.MONTHS);
System.out.println("相隔月数:"+days);
days = date.until(date2, ChronoUnit.DAYS);
System.out.println("相隔天数:"+days);
System.out.println("年份:"+date.getYear());
System.out.println("月份:"+date.getMonth());
System.out.println("日期:"+date.getDayOfMonth());
System.out.println("本周的第几天:"+date.getDayOfWeek().getValue());
System.out.println("今年的第几天:"+date.getDayOfYear());
1.4. LocalDateTime
以上两个日期时间处理类分别只能够用于独立处理时间或者日期,如果需要同时进行处理则可以使用LocalDateTime
,是一个不可变的线程安全的日期时间处理类,可以通过静态的方法now()
获取系统日期时间实例
通过观察源码,LocalDateTime
本质上就是LocalDate
和LocalTime
结合。
1.4.1. 常用方法
-
static LocalDateTime now()
:获取系统日期时间对象 -
static LocalDateTime of(int year,int month,int day,int hour,int minute,int seconds)
:根据提供的参数构建日期时间 -
long unitl(Temporal endExclusive,TemporalUnit unit)
:计算当前日期和目标日期endExclusive
之间的差(单位:unit
)//获取系统时间(包含日期和时间)
//根据提供的日期和时间构建LocalDateTime对象
// 将时间对象分别转换为LocalDate和LocalTime
//toLocalDate()、toLocalTime()
//相差天数,
//将LocalDateTime转换为java.util.Date
// Instand instant = datetime.atZone(ZoneId.systemDefault()).toInstant()
// Date date = Date.from(instant )
//获取系统时间(包含日期和时间)
LocalDateTime now = LocalDateTime.now();
System.out.println(now.toString());
// 将时间对象分别转换为LocalDate和LocalTime
// LocalDate localDate = now.toLocalDate();
// LocalTime localTime = now.toLocalTime();
//根据提供的日期和时间构建LocalDateTime对象
LocalDateTime tm = LocalDateTime.of(2021, 7, 30, 15, 55, 45);
System.out.println(tm);
long until = now.until(tm, ChronoUnit.DAYS);
System.out.println("相差天数:"+until);
until = now.until(tm, ChronoUnit.HOURS);
System.out.println("相差小时数:"+until);
until = now.until(tm, ChronoUnit.MINUTES);
System.out.println("相差分钟数:"+until);
until = now.until(tm, ChronoUnit.SECONDS);
System.out.println("相差秒钟数:"+until);
//如何将LocalDateTime转换为java.util.Date
//获取当前LocalDateTime Instance对象
Instant instant = now.atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);
System.out.println(date);
1.5. DateTimeFormatter
在使用Date和Calendar如如果需要对日期显示格式进行处理,此时我们需要使用DateFormat
&SimpleDateFormat
,这两个都是线程不安全的操作(即多线程并发对日期格式处理时,不能共享该对象);因此,在JDK8中同时提供了用于对新增日期时间类的格式化工具:DateTimeFormatter
DateTimeFormatter
提供了比DateFormat
更为简单API,另外是一个线程安全的日期格式化类。
-
String format(DateTimeFormatter fmt)
:将当前的日期时间对象使用指定的格式化工具,转换为字符串格式 -
static LocalDateTime parse(CharSequence text,DateFormatter fmt)
:将一个字符串按照指定的格式化工具,解析为LocalDateTime//1 将LocalDateTime按照指定的匹配模式格式化为指定的字符串,如yyyy-MM-dd HH:mm:ss
// format()和DateTimeFormatter.ofPattern()
//2 将字符串和指定的匹配格式解析为LocalDateTime 提供时分秒
// parse(strTime,fmt_pattern)
//3 String s1 = "20210708122345"; 转换为2021年07月08日 12时23分45秒
// 分两次:DateTimeFormatter先parse为日期时间格式,再format为字符串 // 思路: String -> LocalDateTime -> String
//1 将LocalDateTime转为字符串:
// 按照指定的匹配模式格式化为指定的字符串,如yyyy-MM-dd HH:mm:ss
// format()和DateTimeFormatter.ofPattern()
LocalDateTime now = LocalDateTime.now();
String s = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("s = " + s);// 2021-12-23 11:03:..
//2 将字符串转为LocalDateTime
// 和指定的匹配格式解析为LocalDateTime 提供时分秒
/* String time = "2020/12/23 23:58:59";
LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
*/
String time = "2020年12月23日 23时58分59秒";
LocalDateTime localDateTime = LocalDateTime.parse(time,
DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒"));
System.out.println("localDateTime = " + localDateTime);
//parse(strTime,fmt_pattern)
//3 String s1 = "20210708122345"; **转换为2021年07月08日 12时23分45秒**
//分两次:DateTimeFormatter先parse为日期时间格式,再format为字符串
// 思路: String -> LocalDateTime -> String
String s1 = "20210708122345";
// 1 先转为LocalDateTime
LocalDateTime localDateTime2 = LocalDateTime.parse(s1,
DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
String str = localDateTime2.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒"));//指定目标格式
System.out.println(s1+"转换为指定日期时间格式 : " + str);