1、Java8之前的日期和时间API
1.1、TestSimpleDateFormat
package com.chenheng;
import com.chenheng.java8.DateFormatThreadLocal;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
@SpringBootTest
public class TestSimpleDateFormat {
@Test
void test01() throws ExecutionException, InterruptedException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Callable<Date> task = () -> sdf.parse("20211128");
ExecutorService pool = Executors.newFixedThreadPool(10);
List<Future<Date>> results = new ArrayList<>();
for (int i = 0; i < 10; i++) {
results.add(pool.submit(task));
}
for (Future<Date> future : results) {
System.out.println(future.get());
}
pool.shutdown();
}
@Test
void test02() throws ExecutionException, InterruptedException {
Callable<Date> task = () -> DateFormatThreadLocal.convert("20211128");
ExecutorService pool = Executors.newFixedThreadPool(10);
List<Future<Date>> results = new ArrayList<>();
for (int i = 0; i < 10; i++) {
results.add(pool.submit(task));
}
for (Future<Date> future : results) {
System.out.println(future.get());
}
pool.shutdown();
}
@Test
void test03() throws ExecutionException, InterruptedException {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
Callable<LocalDate> task = () -> LocalDate.parse("20211128", dtf);
ExecutorService pool = Executors.newFixedThreadPool(10);
List<Future<LocalDate>> results = new ArrayList<>();
for (int i = 0; i < 10; i++) {
results.add(pool.submit(task));
}
for (Future<LocalDate> future : results) {
System.out.println(future.get());
}
pool.shutdown();
}
}
1.2、DateFormatThreadLocal
package com.chenheng.java8;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatThreadLocal {
private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>(){
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyyMMdd");
}
};
public static Date convert(String source) throws ParseException {
return df.get().parse(source);
}
}
2、Java8的日期和时间API
2.1、TestLocalDateTime
package com.chenheng;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;
@SpringBootTest
public class TestLocalDateTime {
@Test
void test08(){
LocalDateTime now = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
System.out.println(now);
LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
System.out.println("ldt2->"+ldt2);
ZonedDateTime zonedDateTime = ldt2.atZone(ZoneId.of("Europe/Tallinn"));
System.out.println("zonedDateTime->"+zonedDateTime);
System.out.println("*****************************");
LocalDateTime shanghaiLdt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
ZonedDateTime shanghaiZonedDateTime = shanghaiLdt.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println("shanghaiZonedDateTime->"+shanghaiZonedDateTime);
}
@Test
void test07(){
Set<String> set = ZoneId.getAvailableZoneIds();
set.forEach(System.out::println);
}
@Test
void test06(){
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
LocalDateTime ldt = LocalDateTime.now();
String strDate = dtf.format(ldt);
System.out.println("strDate->"+strDate);
System.out.println("*****************");
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String strDate2 = dtf2.format(ldt);
System.out.println("strDate2->"+strDate2);
LocalDateTime lastDate = LocalDateTime.parse(strDate2, dtf2);
System.out.println("lastDate->"+lastDate);
}
@Test
void test05(){
LocalDateTime ldt = LocalDateTime.now();
System.out.println("ldt->"+ldt);
LocalDateTime ldt2 = ldt.withDayOfMonth(10);
System.out.println("ldt2->"+ldt2);
LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println("ldt3->"+ldt3);
LocalDateTime ldt5 = ldt.with((l) -> {
LocalDateTime ldt4 = (LocalDateTime) l;
DayOfWeek dayOfWeek = ldt4.getDayOfWeek();
if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
return ldt4.plusDays(3);
} else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
return ldt4.plusDays(2);
} else {
return ldt4.plusDays(1);
}
});
System.out.println("ldt5->"+ldt5);
}
@Test
void test04(){
LocalDate localDate1 = LocalDate.of(2021, 7, 26);
LocalDate now = LocalDate.now();
Period p = Period.between(localDate1, now);
System.out.println(p);
System.out.println("years->"+p.getYears());
System.out.println("months->"+p.getMonths());
System.out.println("days->"+p.getDays());
System.out.println("allDays->"+p.get(ChronoUnit.DAYS));
}
@Test
void test03(){
Instant ins1 = Instant.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
Instant ins2 = Instant.now();
Duration duration = Duration.between(ins1, ins2);
System.out.println("toMillis->"+duration.toMillis());
System.out.println("******************************");
LocalTime localTime1 = LocalTime.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
LocalTime localTime2 = LocalTime.now();
System.out.println(Duration.between(localTime1, localTime2).toMillis());
}
@Test
void test02(){
Instant now = Instant.now();
System.out.println("now->"+now);
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
System.out.println("offsetDateTime->"+offsetDateTime);
System.out.println(now.toEpochMilli());
Instant instant2 = Instant.ofEpochSecond(60);
System.out.println(instant2);
}
@Test
void test01(){
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime ldt = LocalDateTime.of(2021, 11, 28, 20, 53);
LocalDateTime plusMonths = ldt.plusMonths(1);
System.out.println("plusMonths->"+plusMonths);
LocalDateTime minusMonths = ldt.minusMonths(1);
System.out.println("minusMonths->"+minusMonths);
LocalDateTime plusDays = ldt.plusDays(1);
System.out.println("plusDays->"+plusDays);
System.out.println("year->"+ldt.getYear());
System.out.println("month->"+ldt.getMonthValue());
System.out.println("day->"+ldt.getDayOfMonth());
System.out.println("hour->"+ldt.getHour());
System.out.println("minute->"+ldt.getMinute());
}
@Test
void test0101(){
LocalDateTime now = LocalDateTime.now();
LocalDateTime sDateTime = LocalDateTime.of(now.toLocalDate(), LocalTime.MIN);
String startDay = sDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("startDay->"+startDay);
LocalDateTime eDateTime = LocalDateTime.of(now.toLocalDate(), LocalTime.MAX);
String endDay = eDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("endDay->"+endDay);
}
}
3、参考链接
1、Java8日期视频