LocalDate类
方法:
new()//获取当前时间 of(y,M,d) //获取时间对象
ofYearDay(int Year,int dayofYear) //获取哪年第多少天的日期
parse(String) //将字符串转为日期
LocalDate now = LocalDate.now();//获取当前时间。
System.out.println(now);
LocalDate of = LocalDate.of(2019, 12, 25);//获取2019.12.25对象
System.out.println(of);
// TODO 获取2017年第223天的日期
LocalDate localDate = LocalDate.ofYearDay(2019, 223);
System.out.println(localDate);
LocalDate parse = LocalDate.parse("2019-12-25");//字符串转为日期格式
System.out.println(parse);
ValueRange range = parse.range(ChronoField.DAY_OF_MONTH);//12月天数范围
System.out.println(range);
LocalDate localDate1 = now.plusDays(25);
System.out.println(localDate1);
//localDate 和 localDate1相差的天数
long until = localDate.until(localDate1, ChronoUnit.DAYS);
System.out.println(until);
String format = parse.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
// 将date5的日期转化为 day/month/year
System.out.println(format)
LocalTime类
方法
now(); plusHours() ; minusHours(); isBefore() ; isAfter();
format(DateTimeFormatter.ofPattern("hh:mm:ss"));// 将当前时间转化为 hh:mm:ss 格式的字符串
LocalTime currentTime = LocalTime.now();
System.out.println(currentTime);
// 获取伦敦的时间
LocalTime i = LocalTime.now(ZoneId.of("Europe/London"));
System.out.println(i);
// 10个小时20分钟之后是几点
LocalTime localTime = LocalTime.now().plusHours(10).plusHours(20);
System.out.println(localTime);
// 判断localTime时间是否为当前时间之后的时间
boolean after = localTime.isAfter(LocalTime.now());
System.out.println(after);
// 当前时间的前20个小时间是几点
LocalTime beforeTime = currentTime.minusHours(20);
System.out.println(beforeTime);
// 判断beforeTime是否在当前时间之前
boolean before = beforeTime.isBefore(currentTime);
System.out.println(before);
// 将当前时间转化为 hh:mm:ss 格式的字符串
String format = currentTime.format(DateTimeFormatter.ofPattern("hh:mm:ss"));
System.out.println(format);
LocalDateTime
方法
new()//获取当前时间 format(); range() //查看范围
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
String nowStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
System.out.println(nowStr);
// 查看now日期时间的所在月份的天范围
ValueRange range = now.range(ChronoField.DAY_OF_MONTH);
System.out.println(range.getMinimum()+","+range.getMaximum());
// 获取第一天和最后一天的日期
LocalDate first = LocalDate.of(now.getYear(), now.getMonth(),(int)range.getMinimum());
LocalDate last = LocalDate.of(now.getYear(), now.getMonth(),(int)range.getMaximum());
System.out.println(first+"/"+last);
Instan
now() plusSeconds()
ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(zonedDateTime.toLocalDateTime());
Instant now = Instant.now();
now = now.plusSeconds(8*3600); // 增加8个小时的秒数
// 查看时间戳的毫秒值
System.out.println(now.toEpochMilli()+","+now);
System.out.println(System.currentTimeMillis()+","+ new Date(System.currentTimeMillis()));
System.out.println(new Date().getTime()+","+new Date());