packagelocaldatetime;importjava.time.LocalDate;importjava.time.LocalDateTime;importjava.time.LocalTime;publicclassDemo{publicstaticvoidmain(String[] args){LocalDateTime now =LocalDateTime.now();System.out.println(now);System.out.println("----------------------------");int year = now.getYear();//方式一//int month = now.getMonthValue();//方式二int month = now.getMonth().getValue();int day = now.getDayOfMonth();int hour = now.getHour();int minute = now.getMinute();int second = now.getSecond();System.out.println(year +"年"+ month +"月"+ day +"日"+ hour +"时"+ minute +"分"+ second +"秒");System.out.println("----------------------------");//转换后就可以使用各自的方法了LocalDate localDate = now.toLocalDate();System.out.println(localDate);LocalTime localTime = now.toLocalTime();System.out.println(localTime);}}
packageperiod;importjava.time.LocalDate;importjava.time.Period;publicclassDemo{publicstaticvoidmain(String[] args){//当前本地 年月日LocalDate now =LocalDate.now();//生日的 年月日LocalDate localDate =LocalDate.of(1999,2,12);//第二个参数减第一个参数Period between =Period.between(localDate, now);System.out.println("相差的时间间隔的对象:"+between);System.out.println("相差的年数:"+between.getYears());System.out.println("相差的月数:"+between.getMonths());System.out.println("相差的天数:"+between.getDays());System.out.println("相差总月份:"+between.toTotalMonths());}}
packageduration;importjava.sql.SQLOutput;importjava.time.Duration;importjava.time.LocalDate;importjava.time.LocalDateTime;publicclassDemo{publicstaticvoidmain(String[] args){LocalDateTime now =LocalDateTime.now();LocalDateTime localDateTime =LocalDateTime.of(1999,2,12,0,0,0);//第二个参数减第一个参数Duration between =Duration.between(localDateTime, now);System.out.println(between);System.out.println("相差的时间间隔对象"+between);System.out.println("相差的毫秒数"+between.toMillis());System.out.println("相差的分钟数"+between.toMinutes());System.out.println("相差的小时数"+between.toHours());System.out.println("相差的天数"+between.toDays());System.out.println("相差的周数"+between.toDays()/7);System.out.println("相差的月数"+between.toDays()/30);System.out.println("相差的年数"+between.toDays()/365);System.out.println("相差的季度数"+between.toDays()/365/3);}}