LocalDate LocalTime LocalDateTime Instant的操作与使用

 

一.简介  

  LocalDate表示当前(或指定)日期,格式为:yyyy-MM-dd

  LocalTime表示当前(或指定)时间,格式为:HH:mm:ss SSS

  LocalDateTime表示当前(或指定)日期时间,格式为:yyyy-MM-ddTHH:mm:ss SSS ,是前2者的结合

  Instant表示当前(或指定)时间瞬时点,或者瞬间点

 

二.使用介绍  

  jdk1.8之后对表示时间的类型进行了重新编写,表示当前日期时间的有LocalDate、LocalTime、LocalDateTime这三个类,表示瞬间时间点的是Instant

  api提供了时间类型构造方法、getter方法、时间加减操作、时间判断操作、指定未来时间操作、时间支持的类型操作,其使用方法基本上一致。

  LocalDate

  LocalTime   

  LocalDateTime

  Instant

LocalDate

public class LocalDateTimeTest {
    
    public static void main(String[] args) {//============ LoacalDate 的构造  ============
        
        LocalDate localDate = LocalDate.now();                  //获取当前时间:2019-12-07
        LocalDate localDate2 = LocalDate.of(2019, 12, 8);      //根据参数设置日期,参数分别为年,月,日
        
        System.out.println("localDate -----"+localDate);
        System.out.println("localDate2 -----"+localDate2);
        
        //============ LoacalDate 获取当前时间属性  ============
        
        System.out.println(localDate.getYear());              //获取当前年份:2019
        System.out.println(localDate.getMonth());             //获取当前月份,英文:DECEMBER        
        System.out.println(localDate.getMonthValue());         //获取当前月份,数字:12
        
        System.out.println(localDate.getDayOfMonth());         //获取当前日期是所在月的第几天7
        System.out.println(localDate.getDayOfWeek());          //获取当前日期是星期几(星期的英文全称):SATURDAY
        System.out.println(localDate.getDayOfYear());          //获取当前日期是所在年的第几天:341
        
        System.out.println(localDate.lengthOfYear());          //获取当前日期所在年有多少天
        System.out.println(localDate.lengthOfMonth());         //获取当前日期所在月份有多少天
        System.out.println(localDate.isLeapYear());            //获取当前年份是否是闰年
        
        //============ LoacalDate 当前时间的加减  ============
        
        System.out.println(localDate.minusYears(1));           //将当前日期减1年
        System.out.println(localDate.minusMonths(1));          //将当前日期减1月
        System.out.println(localDate.minusDays(1));            //将当前日期减1天
        
        System.out.println(localDate.plusYears(1));            //将当前日期加1年
        System.out.println(localDate.plusMonths(1));           //将当前日期加1月
        System.out.println(localDate.plusDays(1));             //将当前日期加1天
        
        //============ LoacalDate 当前时间的判断 ============
        System.out.println("LoacalDate的判断");
        System.out.println(localDate.isAfter(localDate2));                    //localDate在localDate2日期之后
        System.out.println(localDate.isBefore(localDate2));                    //localDate在localDate2日期之前
        System.out.println(localDate.isEqual(localDate2));                    //localDate和localDate2日期是否相等
        
        //============ LoacalDate 当前时间支持的类型  ============
        System.out.println(localDate.isSupported(ChronoField.DAY_OF_YEAR));    //当前时间支持的时间类型是:一年中的某一天,这个不知道应用场景
        System.out.println(localDate.isSupported(ChronoUnit.DAYS));            //当前日期支持的单元:天(说明当前时间是按天来算的)
        
        System.out.println(localDate.with(TemporalAdjusters.firstDayOfMonth()));            //本月的第1天
        System.out.println(localDate.with(TemporalAdjusters.firstDayOfNextMonth()));        //下月的第1天
        System.out.println(localDate.with(TemporalAdjusters.firstDayOfNextYear()));         //下年的第1天
        
     //============ LocalDate 指定时间的操作  ===============    
        System.out.println(localDate.withDayOfMonth(2));                                    //本月的第几天
        System.out.println(localDate.with(TemporalAdjusters.lastDayOfMonth()));             //本月的最后一天
        System.out.println(localDate.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)));   //上一周星期天是几号
        System.out.println(localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY)));       //下一周星期一是几号
               
}

LocalTime

public static void testLocalTime() {
        
        //============ LocalTime 的构造  ============
        LocalTime localTime = LocalTime.now();                //获取当前时间
        
//        LocalTime.of(int hour, int minute) 根据参数设置时间,参数分别为时,分
//        LocalTime.of(int hour, int minute, int second) 根据参数设置时间,参数分别为时,分,秒
        LocalTime localTime2 = LocalTime.of(18, 18);
        LocalTime localTime3 = LocalTime.of(18, 18,18);
        System.out.println(localTime2);
        System.out.println(localTime3);
        
        //============ LoacalDate 获取当前时间属性  ============
        System.out.println(localTime);
        System.out.println(localTime.getHour());
        System.out.println(localTime.getMinute());
        System.out.println(localTime.getSecond());
        
        System.out.println(localTime.plusHours(1));        //将当前时间加1时
        System.out.println(localTime.plusMinutes(1));    //将当前时间加1分钟
        System.out.println(localTime.plusSeconds(1));    //将当前时间加1秒
        
        System.out.println(localTime.minusHours(1));    //将当前时间减1小时
        System.out.println(localTime.minusMinutes(1));    //将当前时间减1分钟
        System.out.println(localTime.minusSeconds(1));    //将当前时间减1秒
        
}

LocalDateTime

public static void testLocalDateTime() {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
        System.out.println(localDateTime2);
        System.out.println(localDateTime);
        
}

Instant

public static void testInstant() {
        Instant instant = Instant.now();
        System.out.println(instant);
        System.out.println(instant.getNano());             //纳秒数
        System.out.println(instant.getEpochSecond());    //1970年到现在的秒数
        System.out.println(instant.toEpochMilli());     //1970年到现在的毫秒数(和new Date().getTime() System.currentTimeMillis 一样)
        //============ Instant 时间区间的加减 省略,用法基本一致  ============
        
}

 

 

 

 

 

 

 

 

 

 

posted @ 2019-12-08 11:59  硝烟漫过十八岁  阅读(2531)  评论(6编辑  收藏  举报