ZoneId id = ZoneId.systemDefault();
LocalDateTime dateTime = LocalDateTime.now(id);
System.out.println("时区ID:"+id);
System.out.println("当前时间:"+dateTime); LocalDateTime addYear = dateTime.plus(1, ChronoUnit.YEARS);
System.out.println("当前时间加1年:"+addYear); LocalDateTime addDay = dateTime.plusDays(56);
System.out.println("距56天后是几号:"+addDay); System.out.println("从默认时区的系统获取当前日期时间:"+ZonedDateTime.now());
String now = "2018-09-01";
String now1 = "2019-09-01 11:12:23"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDate nowTime = LocalDate.parse(now,formatter);
LocalDateTime nowTime1 = LocalDateTime.parse(now1,formatter1); System.out.println("yyyy-MM-dd 字符串转时间:"+nowTime);
System.out.println("yyyy-MM-dd HH:mm:ss 字符串转时间:"+nowTime1); Long now2 = System.currentTimeMillis();
LocalDateTime date = LocalDateTime.ofEpochSecond(now2/1000,0,ZoneOffset.ofHours(8));
System.out.println("时间戳转时间:"+date); LocalDateTime startDateTime = LocalDateTime.of(2018,9,19,23,34,12);
LocalDateTime endDateTime = LocalDateTime.of(2018,9,15,4,34,12);
System.out.println("手动构造开始时间:"+startDateTime);
System.out.println("手动构造结束时间:"+endDateTime);
Duration duration = Duration.between(endDateTime,startDateTime);
System.out.printf("时间1,时间2相差:%s天%n",duration.toDays());
System.out.printf("时间1,时间2相差:%s分钟%n",duration.toMinutes()); LocalDate startDate = LocalDate.of(2018,10,19);
LocalDate endDate = LocalDate.of(2018,9,15); Period period = Period.between(endDate,startDate);
System.out.printf("日期1,日期2相差:%s天%n",period.getDays()); System.out.println("本月有多少天:"+YearMonth.now().lengthOfMonth());
System.out.println("今年有多少天:"+YearMonth.now().lengthOfYear());