package com.commonClass;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
public class JDK8DateTimeTest {
/**
* jdk8 中日期时间API的测试
*
*/
@Test
public void testDate(){
Date date1 = new Date(2020-1900,9-1,8);//有偏移量,不好理解
System.out.println(date1);//Tue Sep 08 00:00:00 CST 2020
}
/*
LocalDate LocalTime LocalDateTime 的使用
说明:
1.LocalDateTime相较于LocalDate、LocalTime,使用频率较高
2.类似于Calendar
*/
@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(2021, 3, 27, 14, 58, 56);
System.out.println(localDateTime1);
System.out.println("===========================================");
//getXxx() 获取信息
System.out.println(localDateTime.getDayOfMonth());//27
System.out.println(localDateTime.getDayOfWeek());//SATURDAY
System.out.println(localDateTime.getMonth());//MARCH
System.out.println(localDateTime.getMonthValue());//3
System.out.println(localDateTime.getMinute());//42
//体现不可变性 更改日期时间withXxx()
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(22);//更改DayOfMonth
LocalDateTime localDateTime3 = localDateTime.withHour(4);//更改Hour
System.out.println(localDateTime);
System.out.println(localDateTime2);
System.out.println(localDateTime3);
//加 减
LocalDateTime localDateTime4 = localDateTime.plusMonths(4);
System.out.println(localDateTime4);
LocalDateTime localDateTime5 = localDateTime.minusDays(6);
System.out.println(localDateTime5);
}
}
注:
/*
Instant 瞬时
*/
@Test
public void test2(){
Instant now = Instant.now();//now() 获取本初子午线时间
System.out.println(now);//2021-03-27T08:31:55.610Z
//添加时间的偏移量
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));//东八区时间
System.out.println(offsetDateTime);
//获取瞬时点距离1970.0.0.0(UTC)到现在的毫秒数
long milli = now.toEpochMilli();
System.out.println(milli);//1616834314519
//ofEpochMilli() 通过给定的毫秒数获取Instant实例 类似于---->Date(long mills)
Instant instant = Instant.ofEpochMilli(1616834314519L);
System.out.println(instant);
}
/*
DateTimeFormatter:格式化或解析 日期、时间
类似于SimpleDateFormat
*/
@Test
public void test3(){
//方式一:预定义的标准格式
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期----->字符串
LocalDateTime now = LocalDateTime.now();
String str1 = formatter.format(now);
System.out.println(now);
System.out.println(str1);
//解析:字符串----->日期
TemporalAccessor parse = formatter.parse("2021-03-27T17:07:20.104");
System.out.println(parse);
//方式二:
//本地化相关的格式。如:ofLocalizedDateTime()
//FormatStyle.LONG "2021年3月27日 下午09时46分21秒" /FormatStyle.MEDIUM "2021-3-27 21:46:41" /FormatStyle.SHORT "21-3-27 下午9:45" /
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
//格式化
String str2 = formatter1.format(now);
System.out.println("=====================");
System.out.println(str2);
//解析:字符串--->日期
TemporalAccessor parse1 = formatter1.parse("2021-3-27 21:46:41");
System.out.println(parse1);
//本地化相关的格式。如:ofLocalizedDate() 同上,不解释了
//FormatStyle.FULL "2021年3月27日 星期六" /FormatStyle.LONG "2021年3月27日"
//FormatStyle.MEDIUM ”2021-3-27“ /FormatStyle.SHORT "21-3-27" /
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
//格式化
String str3 = formatter2.format(LocalDate.now());
System.out.println("=======================");
System.out.println(str3);
//解析 略.
//重点:【主流方式】
//方式三:自定义方式: 如:ofPatten("yyyy-MM-dd hh:mm:ss")
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println("=======================");
System.out.println(str4);//2021-03-27 10:07:49
//解析:
TemporalAccessor accessor = formatter3.parse("2021-03-27 10:07:49");
System.out.println(accessor);
}