package com.atguigu.java2;
import org.junit.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
/**
* @author lxd
* @create 2021-12-02 14:06
*/
/*
jdk8中日期时间的API
localDate \localTime \localDateTime:获取当前时间
Instant:对用本初子午线
DateTimeFormat:格式化或解析日期。时间
*/
public class JdkDateTimeTest {
@Test
public void testDate() {
// 偏移量
Date date1 = new Date(2020 - 1900, 9 - 1, 8);
System.out.println(date1); //Fri Oct 08 00:00:00 CST 3920
}
@Test
public void test() {
//localDate
//localTime
//localDateTime
//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(2020, 10, 23, 13, 56, 43);
System.out.println(localDateTime1);
// getXxx():
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getMinute());
System.out.println(localDateTime.getHour());
System.out.println(localDateTime.getDayOfYear());
//withXxx():设置;体现不可变性
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(22);
System.out.println(localDateTime);
System.out.println(localDateTime2);
//
LocalDateTime localDateTime3 = localDateTime.withHour(4);
System.out.println(localDateTime);
System.out.println(localDateTime3);
//plusXxx():加,不可变性
// minusXxx():减;不可变性
LocalDateTime localDateTime4 = localDateTime.plusMonths(5);
System.out.println(localDateTime);
System.out.println(localDateTime4);
}
//Instant的使用
@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开始的毫秒数
long l = instant.toEpochMilli();
System.out.println(l);
// ofEpochMilli(): 通过给定的毫秒数,获取Instant实例 --->Date(long millis)
Instant instant1 = Instant.ofEpochMilli(1638427330497L);
System.out.println(instant1);
}
//DateTimeFormat:格式化或解析日期。时间
// 类似于SimpleDateFormat
@Test
public void test3() {
// 方式一:预定义的本地标准格式:ISO_LOCAL_DATE+TIME;ISO_LOCAL_DATE;
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime localDateTime = LocalDateTime.now();
// 格式化
String str1 = formatter.format(localDateTime);
System.out.println(str1);
// 解析
TemporalAccessor parse = formatter.parse(str1);
System.out.println(parse);
// 方式二:本地化相关的格式:如:ofLocalizeDateTime(FormatStyle.LONG)
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
String str = formatter1.format(localDateTime);
System.out.println(str);
TemporalAccessor parse1 = formatter1.parse(str);
System.out.println(parse1);
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
LocalDate now = LocalDate.now();
String str2 = formatter2.format(now);
System.out.println(str2);
TemporalAccessor parse2 = formatter2.parse(str2);
System.out.println(parse2);
// 重点: 方式三:自定义的格式:如:ofPattern("yyyy-MM-dd hh:mm:ss E")
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = formatter3.format(localDateTime);
System.out.println(format);
System.out.println(formatter3.parse(format));
}
}
暂无讨论,说说你的看法吧