jdk8新的日期時間API
package com.zenqgifeng.practicedemo.jdk8newproperties;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
public class Demo08 {
@Test
public void simpleDateFormatTest() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(2019, 01, 02);
System.out.println(date);
String format = sdf.format(date);
for (int i = 0; i < 50; i++) {
new Thread(() -> {
try {
Date date1 = sdf.parse(format);
System.out.println(date1);
} catch (ParseException e) {
e.printStackTrace();
}
}).start();
}
}
@Test
public void lcoalDateTest() {
LocalDate now = LocalDate.now();
System.out.println(now);
LocalDate date = LocalDate.of(2019, 2, 12);
System.out.println(date);
int year = date.getYear();
int monthValue = date.getMonthValue();
int dayOfMonth = date.getDayOfMonth();
System.out.println(year);
System.out.println(monthValue);
System.out.println(dayOfMonth);
date = date.withYear(2078);
date = date.withMonth(4);
date = date.withDayOfMonth(19);
System.out.println(date);
date = date.plusYears(2L);
date = date.plusMonths(10L);
date = date.minusDays(10L);
System.out.println(date);
System.out.println(date.isAfter(now));
System.out.println(date.isBefore(now));
}
@Test
public void test4() {
LocalTime now = LocalTime.now();
LocalTime time = LocalTime.of(13, 12, 1);
System.out.println(now);
System.out.println(time);
time = time.withHour(1);
System.out.println(time);
time = time.withMinute(2);
System.out.println(time);
time = time.withSecond(10);
System.out.println(time);
time = time.plusHours(2);
System.out.println(time);
time = time.plusMinutes(10);
System.out.println(time);
time = time.minusSeconds(3);
System.out.println(time);
}
@Test
public void test3() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日HH時mm分ss秒");
String format = now.format(dtf2);
System.out.println(format);
String dateTime2 = "2020年01月06日15時25分57秒";
LocalDateTime localDateTime = LocalDateTime.parse(dateTime2, dtf2);
System.out.println(localDateTime);
}
@Test
public void testInstant() {
Instant now = Instant.now();
System.out.println(now);
Instant epochSecond = Instant.ofEpochSecond(2);
System.out.println(epochSecond);
long epochSecond1 = now.getEpochSecond();
System.out.println(epochSecond1);
now = now.plusSeconds(3);
System.out.println(now);
now = now.plusMillis(100);
System.out.println(now);
now = now.plusNanos(1000);
System.out.println(now);
now = now.minusSeconds(2);
System.out.println(now);
}
@Test
public void testDuration() {
LocalTime now = LocalTime.now();
LocalTime localTime = LocalTime.of(10, 10, 10);
Duration duration = Duration.between(localTime, now);
long days = duration.toDays();
System.out.println(days);
long hours = duration.toHours();
System.out.println(hours);
long minutes = duration.toMinutes();
System.out.println(minutes);
long millis = duration.toMillis();
System.out.println(millis);
System.out.println(duration.toString());
}
@Test
public void testPeriod() {
LocalDate now = LocalDate.now();
LocalDate date = LocalDate.of(2018, 5, 1);
Period period = Period.between(date, now);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
System.out.println(period.getUnits());
}
@Test
public void testTemporalAdjust() {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime dateTime = now.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println(dateTime);
}
@Test
public void testZonedDateTime() {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
ZonedDateTime utcDateTime = ZonedDateTime.now(Clock.systemUTC());
System.out.println(utcDateTime);
ZonedDateTime zonedDateTime1 = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println(zonedDateTime1);
}
@Test
public void testLocalDateToDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ZonedDateTime now = ZonedDateTime.now();
Date date = Date.from(now.toInstant());
System.out.println(sdf.format(date));
}
}