• java学习day62-Java8新特性--LocalDateTime,LocalDate ,LocalTime 使用


    java8新特性 LocalDateTime,LocalDate ,LocalTime 使用,Period 获取时间差

    转载:https://www.cnblogs.com/gemiaomiao/p/11970661.html

    LocalDateTime使用

    DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    //获取当前时间
    LocalDateTime date=LocalDateTime.now();
    System.out.println("YEAR"+date.getYear());
    System.out.println("MONTH"+date.getMonth());
    System.out.println("DAY"+date.getDayOfMonth());
    System.out.println("HOUR"+date.getHour());
    System.out.println("MINUTES"+date.getMinute());
    System.out.println("SECOND"+date.getSecond());
    
    System.out.println("localDateTime"+dateTimeFormatter.format(date));
    //当前年的第一天
    LocalDateTime firstdayOfYear =date.with(TemporalAdjusters.firstDayOfNextYear());
    System.out.println("firstdayOfYear"+dateTimeFormatter.format(firstdayOfYear));
    //当前年的最后一天
    LocalDateTime enddayOfYear =date.with(TemporalAdjusters.lastDayOfYear());
    System.out.println("enddayOfYear"+dateTimeFormatter.format(enddayOfYear));
    //获取当前月的第一天
    LocalDateTime firstdayOfMonth =date.with(TemporalAdjusters.firstDayOfMonth());
    System.out.println("firstdayOfMonth"+dateTimeFormatter.format(firstdayOfMonth));
    //获取当前月的最后一天
    LocalDateTime endDayOfMonth=date.with(TemporalAdjusters.lastDayOfMonth());
    System.out.println("endDayOfMonth"+dateTimeFormatter.format(endDayOfMonth));
    //获取下个月第一天
    LocalDateTime firstdayOfNextMonth=date.with(TemporalAdjusters.firstDayOfNextMonth());
    System.out.println("firstdayOfNextMonth"+dateTimeFormatter.format(firstdayOfNextMonth));
    //月增加一个月
    LocalDateTime addMonth=date.plusMonths(1);
    System.out.println("addMonth"+dateTimeFormatter.format(addMonth));
    //月减少一个月
    LocalDateTime lessenMonth=date.minusMonths(1);
    System.out.println("lessenMonth"+dateTimeFormatter.format(lessenMonth));
    //天增加一天
    LocalDateTime addOfDay=date.plusDays(1);
    System.out.println("addOfDay"+dateTimeFormatter.format(addOfDay));
    //天减少一天
    LocalDateTime lessOfDay=date.minusDays(1);
    System.out.println("lessOfDay"+dateTimeFormatter.format(lessOfDay));
    

    TemporalAdjusters方法

    TemporalAdjusters.dayOfWeekInMonth() //同一个月中每一周的第几天
    TemporalAdjusters.firstDayOfMonth() //当月的第一天
    TemporalAdjusters.firstDayOfNextMonth() //下月的第一天
    TemporalAdjusters.firstDayOfYear() //当年的第一天
    TemporalAdjusters.firstDayOfNextYear() //明年的第一天
    TemporalAdjusters.firstInMonth() //同一个月中,第一个符合星期几要求的值
    TemporalAdjusters.lastDayOfMonth() //当月的最后一天
    TemporalAdjusters.lastDayOfYear() // 当年的最后一天
    TemporalAdjusters.lastInMonth() //同一个月中,最后一个符合星期几要求的值
    TemporalAdjusters.next() //第一个符合指定星期几要求的值
    TemporalAdjusters.previousOrSame()/nextOrSame() 
    

    LocalDate 获取当前时间

    LocalDate localDate=LocalDate.now();
    System.out.println("localDate"+localDate);
    System.out.println("getYear"+localDate.getYear());
    System.out.println("getMonth"+localDate.getMonth());
    System.out.println("getDayOfMonth"+localDate.getDayOfMonth());
    

    LocalTime 获取当前时间

    LocalTime localTime=LocalTime.now();
    System.out.println("localTime"+localTime);
    

    Period 获取时间差

    Period period=Period.between(LocalDate.of(2014,2,5),LocalDate.of(2014,2,5));
    System.out.println("period"+period);
    
  • 相关阅读:
    HUST第八届程序设计竞赛-G小乐乐打游戏(双bfs)
    HDU-1575-Tr A(矩阵快速幂模板)
    HDU-1061-Rightmost Digit (快速幂模板)
    HihoCoder 1142-三分求极值(三分模板)
    Aizu ITP2_6_A(二分模板)
    Codeforces-938D-Buy a Ticket(最短路设虚拟节点+Dijk优先队列优化)
    POJ-1797-Heavy Transportation(最短路变形)
    HDU-5137-How Many Maos Does the Guanxi Worth(最短路删点)
    POJ-1094-Sorting It All Out (拓扑排序)(判断环和排名是否唯一)
    HDU-1869-六度分离(多源到多源最短路)
  • 原文地址:https://www.cnblogs.com/liqbk/p/13388432.html
Copyright © 2020-2023  润新知