package test;
import org.junit.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class DataTimeUtil {
/**
* 日期操作
*/
@Test
public void test1(){
// 1、创建指定的日期
LocalDate date1 = LocalDate.of(2021, 05, 01);
System.out.println("date1="+date1);
// 2、得到当前日期
LocalDate now = LocalDate.now();
System.out.println("now="+now);
// 3、根据日期对象获取
System.out.println("年="+now.getYear());
System.out.println("月="+now.getMonth().getValue());
System.out.println("日="+now.getDayOfMonth());
System.out.println("星期="+now.getDayOfWeek().getValue());
}
/**
* 时间操作
*/
@Test
public void test2(){
// 1、得到指定时间
LocalTime time = LocalTime.of(12,12,12,121);
System.out.println(time);
// 2、当前时间
LocalTime now = LocalTime.now();
System.out.println(now);
// 3、根据时间对象获取
System.out.println("时="+now.getHour());
System.out.println("分="+now.getMinute());
System.out.println("秒="+now.getSecond());
System.out.println("纳秒="+now.getNano());
}
/**
* 日期时间类
*/
@Test
public void test3(){
// 1、获取指定的日期时间
LocalDateTime localDateTime = LocalDateTime.of(2020,01,01,01,01,01,1111);
System.out.println(localDateTime);
// 2、获取当前
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
// 3、根据日期时间对象获取
System.out.println("年="+now.getYear());
System.out.println("月="+now.getMonth().getValue());
System.out.println("日="+now.getDayOfMonth());
System.out.println("星期="+now.getDayOfWeek().getValue());
System.out.println("时="+now.getHour());
System.out.println("分="+now.getMinute());
System.out.println("秒="+now.getSecond());
System.out.println("纳秒="+now.getNano());
}
/**
* 日期时间的修改
*/
@Test
public void test4(){
// 1、获取当前
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
// 2、修改,不会修改原来的对象(解决了jdk1.7及以前的线程安全问题)
LocalDateTime nowNew = now.withYear(1998);
System.out.println("修改前="+now);
System.out.println("修改后="+nowNew);
// 3、在当前时间日期基础上加上减去对应时间
System.out.println("当前时间后移2天="+now.plusDays(2));
System.out.println("当前时间后移2年="+now.plusYears(2));
System.out.println("当前时间后移6个月="+now.plusMonths(6));
System.out.println("当前时间前移2年="+now.minusYears(2));
}
/**
* 日期时间的比较
*/
@Test
public void test5(){
// 1、获取当前
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime dateTime = LocalDateTime.of(2021,01,01,01,01,01);
System.out.println(dateTime);
// 2、比较
System.out.println(now.isBefore(dateTime));
System.out.println(now.isAfter(dateTime));
System.out.println(now.equals(dateTime));
}
/**
* 日期格式化
*/
@Test
public void test6(){
// 1、获取当前
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
// 指定格式
DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
// 将日期转为指定格式字符串
System.out.println("format="+now.format(isoLocalDateTime));
// 指定格式 yyyy-MM-dd HH:mm:ss
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = now.format(dateTimeFormatter);
System.out.println("format1="+format);
// 将字符串解析为日期时间类型
LocalDateTime parse = LocalDateTime.parse("2020-01-01 11:00:11", dateTimeFormatter);
System.out.println("解析字符串为指定格式:"+parse);
}
/**
* 时间戳
* 统计时间消耗
*/
@Test
public void test7() throws InterruptedException {
Instant now = Instant.now();
System.out.println(now);
// 获取从1970-1-1 00:00:00 到现在的秒、纳秒
System.out.println(now.getNano());
Thread.sleep(5L);
System.out.println("耗时:"+(Instant.now().getNano()-now.getNano()));
}
/**
* 计算日期时间差
*/
@Test
public void test8(){
// 1、获取当前
LocalTime now = LocalTime.now();
System.out.println(now);
// 比较时间差
LocalTime time = LocalTime.of(22,02,12);
Duration between = Duration.between(now, time);
System.out.println("相差小时:"+between.toHours());
// 比较天数差
LocalDate dateNow = LocalDate.now();
LocalDate date = LocalDate.of(2021,07,22);
Period between1 = Period.between(dateNow, date);
System.out.println("相差天数:"+between1.getDays());
}
/**
* 时间校正器
*/
@Test
public void test9(){
LocalDateTime now = LocalDateTime.now();
// 将当前日期调整到下个月1号
TemporalAdjuster adjuster = (temporal)->{
LocalDateTime dateTime = (LocalDateTime) temporal;
LocalDateTime nextMonth = dateTime.plusMonths(1).withDayOfMonth(1);
System.out.println("nextMonth="+nextMonth);
return nextMonth;
};
LocalDateTime nextMonth1 = now.with(adjuster);
System.out.println("nextMonth1="+nextMonth1);
// 我们还可以通过 TemporalAdjusters来实现
LocalDateTime nextMonth2 = now.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("nextMonth2="+nextMonth2);
}
/**
* 时区操作
*/
@Test
public void test10(){
// 1、获取所有时区id
ZoneId.getAvailableZoneIds().forEach(System.out::println);
// 2获取当前时间,使用中国:东八区的时区,比标准早8个小时
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
// 获取标准时间
ZonedDateTime bz = ZonedDateTime.now(Clock.systemUTC());
System.out.println(bz);
// 获取计算机默认时区,创建日期时间
ZonedDateTime now1 = ZonedDateTime.now();
System.out.println(now1);
// 使用指定时区创建时间
ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("America/Cuiaba"));
System.out.println(now2);
}
}