Java8新时间API
作者:互联网
import org.junit.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;
//Arbor 2022/7/4
public class NewTime {
@Test
public void test1(){
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("当前系统时间:"+localDateTime);
LocalDateTime localDateTime3 = localDateTime.minusDays(1);
System.out.println("减一天"+localDateTime3);
// Instant() unix元年,1970年1月1日00:00:00,这个不是系统时间
Instant ins1 = Instant.now();
System.out.println("utc时间"+ins1);
// 这是utc时间和utc差8小时见差
OffsetDateTime odt = ins1.atOffset(ZoneOffset.ofHours(8));
System.out.println("utc时间+8小时"+odt);
System.out.println("1970年1月1日00:00:00至今毫秒值"+ins1.toEpochMilli());//毫秒值
}
@Test
public void test2(){
Instant ins1 = Instant.now();
Instant ins2 = Instant.now();
// Duration计算两个时间之间间隔
// Period计算两个日期之间间隔的
// 计算机的时间
Duration between1 = Duration.between(ins1, ins2);
System.out.println(between1.toMillis());
// 人看的时间
LocalDateTime localDateTime1 = LocalDateTime.now();
LocalDateTime localDateTime2 = LocalDateTime.now();
Duration between2 = Duration.between(localDateTime1, localDateTime2);
System.out.println(between2.toMillis());
LocalDate localDate1 = LocalDate.of(2022,2,2);
LocalDate localDate2 = LocalDate.now();
Period period = Period.between(localDate1,localDate2);
System.out.println(period.getDays());
}
@Test
public void test3(){
// 时间矫正器,设置下一个月,下一个星期几。。
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
// 指定月的天某一天
LocalDateTime withDayOfMonth = localDateTime.withDayOfMonth(10);
System.out.println("指定月的天某一天:"+withDayOfMonth);
LocalDateTime with = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println("下一个SUNDAY:"+with);
LocalDateTime with1 = localDateTime.with((l) -> {
LocalDateTime ldt4 = (LocalDateTime) l;
DayOfWeek dayOfWeek = ldt4.getDayOfWeek();
System.out.println(dayOfWeek);
return ldt4.plusDays(1);
});
System.out.println(with1);
}
@Test
public void Test4(){
// 时间格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE;
LocalDateTime ldt = LocalDateTime.now();
String format = ldt.format(dateTimeFormatter);
System.out.println(format);
// 自定义
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format1 = ldt.format(dateTimeFormatter1);
System.out.println(format1);
//-------------------------------------
String format2 = dateTimeFormatter1.format(ldt);
System.out.println(format2);
}
@Test
public void Test5(){
// 时区
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
// System.out.println("支持的时区:");
// for (String s:availableZoneIds) {
// System.out.println(s);
// }
LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println("指定时区的时间"+now);
ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println("指定时区的时间格式时间"+zonedDateTime);
}
}
标签:localDateTime,System,API,时间,LocalDateTime,println,now,Java8,out 来源: https://www.cnblogs.com/Arborblog/p/16443860.html