java常用api

===============================================LocalDateTime===============================================
ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则
Instant: 用来表示时间线上的一个点(瞬时)
LocalDate: 表示没有时区的日期, LocalDate是不可变并且线程安全的
LocalTime: 表示没有时区的时间, LocalTime是不可变并且线程安全的
LocalDateTime: 表示没有时区的日期时间, LocalDateTime是不可变并且线程安全的
Clock: 用于访问当前时刻、日期、时间,用到时区
Duration: 用秒和纳秒表示时间的数量(长短),用于计算两个日期的“时间”间隔
Period: 用于计算两个“日期”间隔
DateTimeFormatter 格式化

//获取默认时区
ZoneId systemDefault = ZoneId.systemDefault();
//Date转Instant
Instant instant = new Date().toInstant();
//Instant转Date
Date date= Date.from(instant);

//计算日期间隔,获取时间差时,不会计算前一位时间的
Period period = Period.between(LocalDate.now(), LocalDate.now().plusDays(1));
//计算时间间隔,获取时间差时,会计算前一位时间的

Duration duration = Duration.between(LocalDateTime.now(), LocalDateTime.now().plusDays(1));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss");
//时间转字符串
String format = formatter.format(LocalDateTime.now());
//字符串转时间
LocalDateTime localDateTime = LocalDateTime.parse("2019:11:04 17:16:55", formatter);



Date date = Date.from(LocalDateTime.now().atZone( ZoneId.systemDefault()).toInstant());

Date date= Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());

LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
LocalDate localDate = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()).toLocalDate();
 
原文地址:https://www.cnblogs.com/ENU7/p/11692670.html