1、JDK1.8新增日期时间API
JDK1.0 中使用java.util.Date类 —— 第一批日期时间API
JDK1.1引入Calendar类 —— 第二批日期时间API
缺陷:
可变性 : 像日期和时间这样的类应该是不可变的。
偏移性 : Date中 的年份是从1900开始的,而月份都从0开始。
格式化 : 格式化只对Date有用,Calendar则不行。
JDK1.8新增日期时间API --》第三批日期时间API:
LocalDate、LocalTime、LocalDateTime以及DateTimeFormatter 。
2、LocalDate、LocalTime、LocalDateTime
代码示例:
package test5_Local_Date_Time;
import com.sun.deploy.ui.ImageLoaderCallback;
import java.sql.SQLOutput;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Test1 {
public static void main(String[] args) {
//1、完成实例化(有两种方法)
//方法1、now() ---- 获取当前的日期、时间、日期+时间
LocalDate localDate = LocalDate.now();//日期
System.out.println(localDate);
LocalTime localTime = LocalTime.now();//时间
System.out.println(localTime);
LocalDateTime localDateTime = LocalDateTime.now();//日期+时间
System.out.println(localDateTime);
//方法2、of() --- 设置指定的日期、时间、日期+时间 (且设置好后,不会偏移)
LocalDate of = LocalDate.of(2022,8,1);
LocalTime of1 = LocalTime.of(23,15,58);
LocalDateTime of2 = LocalDateTime.of(2020,5,12,19,35,56,998);
System.out.println(of2);
//LocalDate,LocalTime用的不如LocalDateTime多
//下面讲解用LocalDateTime:
//一系列常用的get()方法:
System.out.println(localDateTime.getYear());
System.out.println(localDateTime.getMonth());//OCTOBER
System.out.println(localDateTime.getMonthValue());//10
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());//SATURDAY
System.out.println(localDateTime.getHour());
System.out.println(localDateTime.getMinute());
System.out.println(localDateTime.getSecond());
System.out.println(localDateTime.getNano());//毫秒:nanoOfSecond ??
//不是set()方法,叫with方法
//体会:不变性。
//(注:之前的/原先的不能改变,只能改变一个新创建的对象--不同于Calendar)
//localDateTime.withMonth(12);
LocalDateTime localDateTime1 = localDateTime.withMonth(12);
System.out.println(localDateTime);//原先的改不了
System.out.println(localDateTime1);
//提供了加减的操作:
//加:
LocalDateTime localDateTime2 = localDateTime.plusMonths(1);
System.out.println(localDateTime);
System.out.println(localDateTime2);
//减:
LocalDateTime localDateTime3 = localDateTime.minusMonths(5);
System.out.println(localDateTime);
System.out.println(localDateTime3);
}
}
3、DateTimeFormatter
一种格式化的类,有三种方式。
(参考之前的SimpleDateFormat —— 用于类型转换,但是1.8后就不能用这个了)
用于LocalDate、LocalTime、LocalDateTime和String类型的转换。
代码示例:
package test5_Local_Date_Time;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
//方式三 —— 重点,以后常用
public class Test2_DateTimeFormatter {
public static void main(String[] args) {
//格式化类:DateTimeFormatter
//方式一、预定义的标准格式。
//三个常量??如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DTAE;ISO_LOCAL_TIME。
DateTimeFormatter df1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//df1就可以帮我们完成LocalDateTime和String之间的相互转换:
//LocalDateTime-->String:
LocalDateTime now = LocalDateTime.now();
String str = df1.format(now); //format()方法同SimpleDateFormat
System.out.println(now); // format()方法要求传入的是 TemporalAccessor Temporal 对象
System.out.println(str); //见下:
//LocalDateTime implements Temporal (可点进去查看)
//Temporal extends TemporalAccessor
//所以,简单理解为: LocalDateTime简洁继承TemporalAccessor —— 所以,可以直接传入now
//String -->LocalDateTime:
TemporalAccessor parse = df1.parse("2022-10-08T15:57:03.69");//format()方法同SimpleDateFormat
//快捷键生成:TemporalAccessor parse —— 理解为:用一个接口去接一个实现类
System.out.println(parse); //相当于调用了toString()方法,--格式并不好看
//方式二:本地化相关的格式。如: oflocalizedDateTime()
//参数:FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT]
//1)FormatStyle.LONG —— 2022年10月8日 下午04时27分50秒
//2)FormatStyle.MEDIUM —— 2022-10-8 16:29:46
//3)FormatStyle.SHORT —— 22-10-8 下午4:30
DateTimeFormatter df2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
//LocalDateTime-->String
LocalDateTime now1 = LocalDateTime.now();
String str2 = df2.format(now1);
System.out.println(str2);
//String -->LocalDateTime:
//注:传入的String字符串的格式要和上面的FormatStyle.LONG、MEDIUM、SHORT一一对应
//TemporalAccessor parse1 = df2.parse("2022年10月8日 下午04时32分11秒");//FormatStyle.LONG
TemporalAccessor parse1 = df2.parse("2022-10-8 16:29:46");//FormatStyle.MEDIUM
//TemporalAccessor parse1 = df2.parse("22-10-8 下午4:30"); //FormatStyle.SHORT
System.out.println(parse1);
//以上两种,格式都是固定好的,不能自定义
//方式三: 自定义的格式。如: ofPattern( "yyyy-MM-dd hh:mm:ss") ---》重点,以后常用
DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//LocalDateTime-->String
LocalDateTime now2 = LocalDateTime.now();
String str3 = df3.format(now2);
System.out.println(str3);//2022-10-08 04:43:56
//String -->LocalDateTime
TemporalAccessor parse2 = df3.parse("2022-10-08 04:43:56");
System.out.println(parse2);//输出较多
}
}