JDK1.8 LocalDateTime
2019-12-23 本文已影响0人
忘记_3a6a
LocalDateTime rightNow=LocalDateTime.now();
System.out.println("当前时间:"+rightNow);
System.out.println(String.format("某一年的第%s天",rightNow.getDayOfYear()));
System.out.println(String.format("当前月份:"+rightNow.getMonth()));
System.out.println(String.format("%s年",rightNow.getYear()));
System.out.println(String.format("%s月",rightNow.getMonthValue()));
System.out.println(String.format("%s日",rightNow.getDayOfMonth()));
System.out.println(String.format("%s时",rightNow.getHour()));
System.out.println(String.format("%s分",rightNow.getMinute()));
System.out.println(String.format("%s秒",rightNow.getSecond()));
System.out.println(String.format("%s纳秒",rightNow.getNano()));
System.out.println(String.format("%s星期",rightNow.getDayOfWeek()));
LocalDateTime ofTime=LocalDateTime.of(2019,12,12,10,9,30);
System.out.println("自定义时间:"+ofTime);
System.out.println("减法");
System.out.println(ofTime.minusYears(1));
System.out.println("加法");
System.out.println(ofTime.plus(20, ChronoUnit.NANOS));
System.out.println(rightNow.format(DateTimeFormatter.ISO_DATE));
System.out.println(rightNow.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
System.out.println(LocalDateTime.parse("2019-12-12 10:09:30",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));