一、LocalDate/LocalTime/LocalDateTime
1、完成实例化
(1)、now()--获取当前的日期,时间,日期+时间
//方法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,5,6);
System.out.println(of);
LocalTime of1 = LocalTime.of(12, 35, 56);
System.out.println(of1);
LocalDateTime of2 = LocalDateTime.of(1890, 12, 23, 13, 24, 59);
System.out.println(of2);
2、LocalDateTime:
LocalDate,LocalTime用的不如LocalDateTime多
(1)get方法:
System.out.println(localDateTime.getYear());//2022
System.out.println(localDateTime.getMonth());//OCTOBER十月
System.out.println(localDateTime.getMonthValue());//10
System.out.println(localDateTime.getDayOfMonth());//20
System.out.println(localDateTime.getDayOfWeek());//THURSDAY
System.out.println(localDateTime.getHour());//23
System.out.println(localDateTime.getMinute());//19
System.out.println(localDateTime.getSecond());//38
(2)无set方法,叫with
localDateTime.withMonth(8);//将月份直接改为8
LocalDateTime localDateTime2 = localDateTime.withMonth(8);
System.out.println(localDateTime);
System.out.println(localDateTime2);
(3)加减操作
在当前月份上进行加减,超过当前月份直接跨年
LocalDateTime localDateTime1 = localDateTime.plusMonths(4);
//加:
System.out.println(localDateTime);
System.out.println(localDateTime1);
//减:
LocalDateTime localDateTime3 = localDateTime.minusMonths(5);
System.out.println(localDateTime3);
二、DateTimeFormatter
完成LocalDateTime和String之间的相互转换
1、预定义的标准格式
eg:ISO_LOCAL_DATE_TIME;/ISO_LOCAL_DATE;/IS0_LOCAL_TIME/
DateTimeFormatter df1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//df1就可以帮我们完成LocalDateTime和String之间的相互转换:
//LocalDateTime--->String:
LocalDateTime now = LocalDateTime.now();
String str = df1.format(now);
System.out.println(str);//2022-10-20T23:33:24.628
//String--->LocalDateTime
TemporalAccessor parse = df1.parse("2022-10-20T23:33:24.628");
System.out.println(parse);
2、本地化相关的格式
eg: oflocalizedDateTime()
参数:FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT
在/String--->LocalDateTime过程中注意传入parse数值要和df2的对象中FormatStyle.***后的对应
//FormatStyle.LONG:2022年10月20日 下午11时38分53秒
//FormatStyle.MEDIUM:2022-10-20 23:39:44
//FormatStyle.SHORT:22-10-20 下午11:40
DateTimeFormatter df2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
//LocalDateTime--->String:
LocalDateTime now1 = LocalDateTime.now();
String str2 = df2.format(now1);
System.out.println(str2);
//String--->LocalDateTime
TemporalAccessor parse1 = df2.parse("22-10-20 下午11:40");//注意传入数值要和FormatStyle.后的对应
System.out.println(parse1);
3、自定义的格式(重要,常用)
eg: ofPattern( "yyyy-MM-dd hh:mm:ss")括号中内容要严格按照日期的标准格式输入
DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//LocalDateTime--->String:
LocalDateTime now2 = LocalDateTime.now();
String forma = df3.format(now2);
System.out.println(forma);//2022-10-20 11:47:40
//String--->LocalDateTime
TemporalAccessor parse2 = df3.parse("2022-10-27 06:22:03");
System.out.println(parse2);
三、导包
在使用IDEA中,编译器会自动导包。无需手动输入
1、part(一)代码
package com.msb.test02;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Test09 {
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,5,6);
System.out.println(of);
LocalTime of1 = LocalTime.of(12, 35, 56);
System.out.println(of1);
LocalDateTime of2 = LocalDateTime.of(1890, 12, 23, 13, 24, 59);
System.out.println(of2);
//LocalDate,LocalTime用的不如LocalDateTime多
//下面讲解用LocalDateTime:
//一些列常用的get***
System.out.println(localDateTime.getYear());//2022
System.out.println(localDateTime.getMonth());//OCTOBER十月
System.out.println(localDateTime.getMonthValue());//10
System.out.println(localDateTime.getDayOfMonth());//20
System.out.println(localDateTime.getDayOfWeek());//THURSDAY
System.out.println(localDateTime.getHour());//23
System.out.println(localDateTime.getMinute());//19
System.out.println(localDateTime.getSecond());//38
//不是set方法,叫with
//体会:不可变性
LocalDateTime localDateTime2 = localDateTime.withMonth(8);
System.out.println(localDateTime);
System.out.println(localDateTime2);
//提供了加减的操作:
LocalDateTime localDateTime1 = localDateTime.plusMonths(4);
System.out.println(localDateTime);
System.out.println(localDateTime1);
//减:
LocalDateTime localDateTime3 = localDateTime.minusMonths(5);
System.out.println(localDateTime3);
}
}
2、part(二)代码
package com.msb.test02;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
public class Test10 {
public static void main(String[] args) {
//格式化类:DateTimeFormatter
//方式一:预定义的标准格式。如: ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;IS0_LOCAL_TIME
DateTimeFormatter df1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//df1就可以帮我们完成LocalDateTime和String之间的相互转换:
//LocalDateTime--->String:
LocalDateTime now = LocalDateTime.now();
String str = df1.format(now);
System.out.println(str);//2022-10-20T23:33:24.628
//String--->LocalDateTime
TemporalAccessor parse = df1.parse("2022-10-20T23:33:24.628");
System.out.println(parse);
//方式二:本地化相关的格式。如: oflocalizedDateTime()
//参数:FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT
//FormatStyle.LONG:2022年10月20日 下午11时38分53秒
//FormatStyle.MEDIUM:2022-10-20 23:39:44
//FormatStyle.SHORT:22-10-20 下午11:40
DateTimeFormatter df2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
//LocalDateTime--->String:
LocalDateTime now1 = LocalDateTime.now();
String str2 = df2.format(now1);
System.out.println(str2);
//String--->LocalDateTime
TemporalAccessor parse1 = df2.parse("22-10-20 下午11:40");//注意传入数值要和FormatStyle.后的对应
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 forma = df3.format(now2);
System.out.println(forma);//2022-10-20 11:47:40
//String--->LocalDateTime
TemporalAccessor parse2 = df3.parse("2022-10-27 06:22:03");
System.out.println(parse2);
}
}