package A_ShangGuiGu.DateTimeTest;
import org.junit.Test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class JKD8新时间API的使用 {
/*
localDate localTime localDateTime 的使用
*/
@Test
public void test1(){
//.now();方法获取当前的日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);//2022-11-19T17:24:05.946
//.of();设置指定的日期时间:年月日时分秒等
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 12, 12, 23, 23, 23);
System.out.println(localDateTime1);
//getXxx()获取这个数据中某个具体的信息
System.out.println(localDateTime.getDayOfMonth());//19(今天是这个月的第多少天)
System.out.println(localDateTime.getDayOfYear());//323(今天是今年的第多少天)
System.out.println(localDateTime.getDayOfWeek());//SATURDAY(周六)
System.out.println(localDateTime.getHour());//17
System.out.println(localDateTime.getYear());//2022
//withXxx();修改,返回一个新的日期或时间类。
LocalDate localDate111 = localDate.withYear(2222);
System.out.println(localDate);//2022-11-19
System.out.println(localDate111);//2222-11-19
//plusXxx();给日期时间做加法,加几个小时、天、周、月、年等。返回一个新的值
System.out.println(localDate);//2022-11-19
LocalDate localDate1 = localDate.plusYears(10);
System.out.println(localDate1);//2032-11-19
//minusXxx();给日期时间做减法,减去几个小时、天、周、月、年等。返回一个新的值。
LocalDate localDate2 = localDate1.minusYears(20);
System.out.println(localDate2);//2012-11-19
}
}
标签:LocalDate,JKD8,System,API,时间,localDateTime,println,localDate,out From: https://www.cnblogs.com/zhazhawei906/p/16906612.html