package java0;
import org.junit.Test;
import sun.plugin.dom.exception.InvalidStateException;
import java.time.*;
import java.time.format.DateTimeFormatter;
/**
*
*
*
* @create 2022-03-23 10:43
*/
public class JDK8DateTimeTest {
/*
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);
//of():设置指定的年月日时分秒,没有偏移量
LocalDateTime localDateTime1=LocalDateTime.of(2022,3,23,10,50,1);
System.out.println(localDateTime1);
//getXxx():获取相关的属性
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getMonth());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getMinute());
//withXxx():设置相关的属性
LocalDate localDate1=localDate.withDayOfMonth(11);
System.out.println(localDate);
System.out.println(localDate1);
}
@Test
public void test2(){
//now()获取本初子午线对应的标准时间
Instant instant=Instant.now();
System.out.println(instant);
//添加时间的偏移量
OffsetDateTime offsetDateTime=instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 --》Date类的getTime()
long milli= instant.toEpochMilli();
System.out.println(milli);
//通过给定的毫秒数,获取instant实例
Instant instant1=Instant.ofEpochMilli(213423424234L);
System.out.println(instant1);
}
/*
DateTimeFormatter:格式化或解析日期、时间
*/
@Test
public void test3(){
DateTimeFormatter formatter=DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime localDateTime=LocalDateTime.now();
String str1=formatter.format(localDateTime);
System.out.println(localDateTime);
System.out.println(str1);
}
}