### 新的日期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 分布式锁如何实现