### 新的日期API
```
@Test
public void test() {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
System.out.println(now.getYear());
System.out.println(now.getMonth());
System.out.println(now.getDayOfMonth());
System.out.println(now.getHour());
System.out.println(now.getMinute());
System.out.println(now.getSecond());
System.out.println(now.getNano());
// 初始化日期
LocalDateTime localDateTime = LocalDateTime.of(2019,12,10,8,30);
System.out.println(localDateTime);
// 加10天
localDateTime = localDateTime.plusDays(10);
System.out.println(localDateTime);
// 減少2天
localDateTime = localDateTime.minusYears(2);
System.out.println(localDateTime);
}
@Test
public void test2() {
// 時間戳 1970年1月1日00:00:00 到某一個時間點的毫秒值
// 默認獲取UTC時區
Instant now = Instant.now();
System.out.println(now);
// 時區設置
long epochMilli = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println(epochMilli);
System.out.println(System.currentTimeMillis());
System.out.println(Instant.now().toEpochMilli());
System.out.println(Instant.now().atOffset(ZoneOffset.ofHours(8)).toInstant().toEpochMilli());
}
@Test
public void test3() throws InterruptedException {
// Duration:計算兩個時間之間的間隔
// Period:計算兩個日期之間的間隔
Instant start = Instant.now();
Thread.sleep(800);
Instant end = Instant.now();
Duration between = Duration.between(start, end);
System.out.println(between);
System.out.println(between.toMillis()); // 毫秒
System.out.println(between.getSeconds()); // 秒
System.out.println(between.toNanos()); // 納秒
System.out.println("------------------");
LocalTime start2 = LocalTime.now();
Thread.sleep(800);
LocalTime end2 = LocalTime.now();
Duration be = Duration.between(start2, end2);
System.out.println(be);
System.out.println(be.toMillis());
System.out.println(be.getSeconds());
System.out.println(be.getNano());
}
@Test
public void test4() throws InterruptedException{
LocalDate end = LocalDate.now();
LocalDate start = LocalDate.of(2018, 5, 25);
Period between = Period.between(start, end);
System.out.println(between);
System.out.println(between.getDays()); // 相隔天數
System.out.println(between.getUnits()); // 單位
System.out.println(between.getMonths()); // 相隔月份
System.out.println(between.getYears()); // 相隔年
System.out.println(between.get(between.getUnits().get(2)));
System.out.println(between.getChronology());
}
@Test
public void test5() {
// temperalAdjust 時間校驗器 例如獲取下周日 下一個工作日
LocalDateTime now = LocalDateTime.now();
System.out.println("本月第一天:"+now.withDayOfMonth(1)); // 本月第一天
System.out.println("本年第一天:"+now.withDayOfYear(1)); // 本年第一天
System.out.println("本年第二月:"+now.withMonth(2)); // 本年第二月
System.out.println("下個星期四:"+now.with(TemporalAdjusters.next(DayOfWeek.THURSDAY))); // 下個星期四
System.out.println("上個星期天:"+now.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY))); // 上個星期天
// LocalDateTime plusDays = now.plusDays(1);
// 獲取下個工作日
LocalDateTime nextWorkDay = now.with((t)->{
LocalDateTime n = (LocalDateTime)t;
DayOfWeek dayOfWeek = n.getDayOfWeek();
if(dayOfWeek.equals(DayOfWeek.FRIDAY)) {
n = n.plusDays(3);
}else if(dayOfWeek.equals(DayOfWeek.SATURDAY)) {
n = n.plusDays(2);
}else {
n = n.plusDays(1);
}
return n;
});
System.out.println("下個工作日:"+nextWorkDay);
}
@Test
public void test7() {
// DateTimeFormatter: 格式化時間/日期
// 自定義格式
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(now.format(ofPattern));
System.out.println(ofPattern.format(now));
DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;
System.out.println(LocalDateTime.now().format(isoDateTime));
LocalDateTime now2 = LocalDateTime.now();
LocalDateTime parse = LocalDateTime.parse("2020-01-02 15:12:22", ofPattern);
System.out.println("當前LocalDateTime對象轉換成String: "+now2.format(ofPattern));
System.out.println("指定的時間轉換成LocalDateTime: "+parse);
}
// ZoneTime ZoneDate ZoneDateTime
@Test
public void test8() {
LocalDateTime now = LocalDateTime.now(ZoneId.of("America/Chicago"));
System.out.println(now);
LocalDateTime now2 = LocalDateTime.now();
ZonedDateTime atZone = now2.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(atZone);
Set<String> s = ZoneId.getAvailableZoneIds();
s.forEach(System.out::println);
}
@Test
public void test9() {
// 5月第一個星期天
LocalDate day = LocalDate.parse("2020-05-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY));
System.out.println(day);
// 本月第三個星期五
LocalDate day2 = LocalDate.now().with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.FRIDAY));
System.out.println(day2);
}
```
*****
原文鏈接:https://blog.csdn.net/qq_29411737/article/details/80835658
- 2111總結
- 1.面向對象
- 1.0.1 super()與this()的區別
- 1.0.2 private、default、protected、public的訪問范圍
- 1.0.3 continue、break、return區別
- 1.0.4 重載和重寫的區別
- 1.0.5 final的特點
- 1.0.6 抽象類與接口的區別
- 1.0.7 java類型
- 1.0.8 什么是反射
- 1.0.9 類的加載機制
- 1.1.1 jvm內存結構
- 1.1.2 java垃圾回收機制
- 1.1.3 并發問題
- 1.1.3.1 線程的狀態與關系
- 1.1.3.2 并發的三大性質
- 1.1.3.3 線程的實現與使用
- 1.1.3.4 線程池相關
- 1.1.3.5 并發相關方法
- 1.1.3.6 線程相關工具
- 1.1.4 jdk8特性
- 1.1.4.1 lambad表達式的使用
- 1.1.4.2 stream API
- 1.1.4.3 Optional容器使用
- 1.1.4.4 LocalDateTime
- 1.15 io流
- 1.16 動態代理實現
- 2.JavaEE
- 2.0.1 JSP四大作用域九大內置對象
- 2.0.2 cookie與session的區別
- 4.數據庫相關
- 5.git版本管理
- 7.一些問題解決
- 7.1 分布式鎖如何實現