本地化日期时间API:
LocalDate LocalTime LocalDateTime
LocalDate的方法:now() , of() , ofYearDay() , parse() , range() , plusDays() , minusDays() , until() , format(DateTimeFormatter.ofPattern) ,
//TODO 获取当前时间
LocalDate now = LocalDate.now();
System.out.println(now);
//TODO 获取2019年12月21日 时间对象
LocalDate of = LocalDate.of(2019, 12, 21);
System.out.println(of);
//TODO 获取2017年第223天的日期
LocalDate localDate = LocalDate.ofYearDay(2017, 223);
System.out.println(localDate);
//将标准的yyyy-MM-dd 字符串转化为日期格式
LocalDate parse = LocalDate.parse("2019-02-12");
System.out.println(parse);
//查看parse日期的2月份天的范围
ValueRange range = parse.range(ChronoField.DAY_OF_MONTH);
System.out.println(range);
//查看parse年的天数
ValueRange range1 = parse.range(ChronoField.DAY_OF_YEAR);
System.out.println(range1);
//查看pares的后20天的日期
LocalDate localDate1 = parse.plusDays(20);
System.out.println(localDate1);
//查看parse的前15天的日期
LocalDate localDate2 = parse.minusDays(15);
System.out.println(localDate2);
//查看localDate1 和 localDate2 之间相差的天数
Period until = localDate2.until(localDate1);
System.out.println(until);
//将localDate2的日期转化为 day/month/year
String format= localDate2.format(
DateTimeFormatter.ofPattern("dd//MM/yyyy")
);
System.out.println(format);
}
LocalTime的核心方法:now() , plusHours() , isAfter() , minusHours() , isBefore() , format()
LocalTime now2 = LocalTime.now();
System.out.println(now2);
// 获取伦敦的时间
LocalTime now3 = LocalTime.now(ZoneId.of("Europe/London"));
// 10个小时20分钟之后是几点
LocalTime localTime = now2.plusHours(10).plusMinutes(20);
System.out.println(localTime);
// 判断 delayTime时间是否为当前时间之后的时间
System.out.println(localTime.isAfter(now2));
// 当前时间的前20个小时间是几点
LocalTime localTime1 = now2.minusHours(20);
System.out.println(localTime1);
// 判断beforeTime是否在当前时间之前
System.out.println(localTime.isBefore(now2));
// 将当前时间转化为 hh:mm:ss 格式的字符串
String format1 = now2.format(DateTimeFormatter.ofPattern("hh:mm:ss"));
System.out.println(format1);
LocalDateTime的核心方法:now() , of() , format() , range.getMaximum() , range.getMinmum()
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
//转化日期格式
String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss "));
System.out.println(format);
//查看now日期时间得所在月份的天范围
ValueRange range = now.range(ChronoField.DAY_OF_MONTH);
System.out.println(range.getMinimum()+"-"+range.getMaximum());
//获取第一天和最后一天的时间
LocalDate max = LocalDate.of(now.getYear(), now.getMonth(), (int) range.getMaximum());
LocalDate min = LocalDate.of(now.getYear(), now.getMonth(), (int) range.getMinimum());
System.out.println(min+"-"+max);
Instant获取时间戳:now() , atZone()
//获取时间戳
Instant now1 = Instant.now();
System.out.println(now1.toEpochMilli());
//获取时间戳 更改时区
Instant instant = Instant.now().atZone(ZoneId.of("Asia/Shanghai")).toInstant();
//查看时间戳的毫秒值
System.out.println(instant.toEpochMilli());
System.out.println(System.currentTimeMillis());
System.out.println(new Date().getTime()+","+new Date());
相互转换 :Date->Instant->LocalDateTime
Date date = new Date();
Instant instant1 = date.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault());
相互转化: LocalDateTime->Instant->Date
Date from = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());