zoukankan      html  css  js  c++  java
  • JDK8日期时间操作小汇总

    统一使用java.time.*包下的类

    1、获取当前的日期、时间、日期加时间

    LocalDate todayDate = LocalDate.now(); //今天的日期
    
    
    LocalTime now = LocalTime.now(); //此刻的时间--精确到毫秒
    
    
    LocalDateTime now = LocalDateTime.now(); //此刻时间--年-月-日-时-分-秒-毫秒
    

     2、获取单独年日月时分秒

    LocalDateTime now = LocalDateTime.now();
    System.out.println("年:"+now.getYear());
    System.out.println("月:"+now.getMonthValue());
    System.out.println("日:"+now.getDayOfMonth());
    System.out.println("时:"+now.getHour());
    System.out.println("分:"+now.getMinute());
    System.out.println("秒:"+now.getSecond());
    System.out.println("该日期是该年的第"+now.getDayOfYear()+"天");
    

    3、将特定的时间转成LocalDate、LocalTime、LocalDateTime  都有很多的重载的方法

    LocalDate of1 = LocalDate.of(2019, 1, 16);
    System.out.println(of1);
    LocalTime of2 = LocalTime.of(9, 25, 36);
    System.out.println(of2);
    LocalDateTime of3 = LocalDateTime.of(of1, of2);
    System.out.println(of3);
    LocalDateTime of4 = LocalDateTime.of(2019, 1, 14, 9, 26, 25);
    System.out.println(of4);
    

     4、判断时间是否相等(日期、时间、日期加时间)时间的比较精确到纳秒,可以先将时间进行格式化,然后比较,可以控制精度问题

    // 日期
    LocalDate date1 = LocalDate.of(2019, 1, 17);
    LocalDate date2 = LocalDate.now();
    if (date1.equals(date2)) {
        System.out.println("日期相等");
    } else {
        System.out.println("日期不相等");
    }
    // 时间
    LocalTime date1 = LocalTime.of(9, 46);
    LocalTime date2 = LocalTime.now();
    if (date1.equals(date2)) {
        System.out.println("时间相等");
    } else {
        System.out.println("时间不相等");
    }
    // 时间加日期
    LocalDateTime date1 = LocalDateTime.of(2019,1,17,9,47);
    LocalTime date2 = LocalTime.now();
    if (date1.equals(date2)) {
        System.out.println("时间加日期相等");
    } else {
        System.out.println("时间加日期不相等");
    }
    

     5、检查周几这种周期性的事件

    DayOfWeek of = DayOfWeek.THURSDAY;
    DayOfWeek from = DayOfWeek.from(LocalDate.now());
    if (from.equals(of)) {
        System.out.println("今天是周四");
    } else {
        System.out.println("今天不是周四");
    }
    DayOfWeek from1 = DayOfWeek.from(LocalDate.of(2019, 1, 10));
    if (from1.equals(of)) {
        System.out.println("该日期是周四");
    } else {
        System.out.println("该日期不是周四");
    }
    

     6、指定时间之后或之前XXX的时间

    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间"+now);
    System.out.println("当前时间之前2天:"+now.minusDays(2));
    System.out.println("当前时间之后2天:"+now.plusDays(2));
    System.out.println("当前时间之前2周:"+now.minusWeeks(2));
    System.out.println("当前时间之后2周:"+now.plusWeeks(2));
    System.out.println("当前时间之前2月:"+now.minusMonths(2));
    System.out.println("当前时间之后2月:"+now.plusMonths(2));
    System.out.println("当前时间之前2年:"+now.minusYears(2));
    System.out.println("当前时间之后2年:"+now.plusYears(2));
    System.out.println("当前时间之前2小时:"+now.minusHours(2));
    System.out.println("当前时间之后2小时:"+now.plusHours(2));
    System.out.println("当前时间之前2分钟:"+now.minusMinutes(2));
    System.out.println("当前时间之后2分钟:"+now.plusMinutes(2));
    System.out.println("当前时间之前2秒:"+now.minusSeconds(2));
    System.out.println("当前时间之后2秒:"+now.plusSeconds(2));
    

     7、比较时间的先后

    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:"+now);
    LocalDateTime appointTime = LocalDateTime.of(2019, 1, 17, 9, 32, 12);
    System.out.println("指定时间:"+appointTime);
    System.out.println(now.isAfter(appointTime));
    System.out.println(now.isBefore(appointTime));
    

     8、格式化时间格式及按照一定格式解析

    LocalDateTime now = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("HH:mm:ss");
    //格式化
    String format = now.format(formatter);
    String format2 = now.format(formatter2);
    String format3 = now.format(formatter3);
    System.out.println("格式化1:" + format);
    System.out.println("格式化2:" + format2);
    System.out.println("格式化3:" + format3);
    //解析
    LocalDateTime localDateTime = LocalDateTime.parse(format, formatter);
    LocalDate localDate = LocalDate.parse(format2, formatter2);
    LocalTime localTime = LocalTime.parse(format3, formatter3);
    System.out.println("解析1:" + localDateTime);
    System.out.println("解析2:" + localDate);
    System.out.println("解析3:" + localTime);
    

    9、根据指定的毫秒数获取LocalDateTime对象

    Long nowMilli = new Date().getTime();
    LocalDateTime localDateTime = Instant.ofEpochMilli(nowMilli).atZone(ZoneId.of("+8")).toLocalDateTime();
    System.out.println(localDateTime);
    

    注:+8 表示东八区,即常说的北京时间

    10、通过LocalDateTime对象获取毫秒数

    LocalDateTime now = LocalDateTime.now();
    long l = now.atZone(ZoneId.of("+8")).toInstant().toEpochMilli();
    System.out.println(l);
    

    注:+8 表示东八区,即常说的北京时间

    11、通过LocalDateTime对象获取秒数

    LocalDateTime now = LocalDateTime.now();
    long second = now.toEpochSecond(ZoneOffset.of("+8"));
    System.out.println(second);
    

     注:+8 表示东八区,即常说的北京时间

    12、判断指定日期是否是闰年(只LocalDate)

    LocalDate now = LocalDate.now();
    if (now.isLeapYear()) {
        System.out.println("now是闰年");
    } else {
        System.out.println("now不是闰年");
    }
    

    13、使用默认格式解析时间(LocalDateTime对象可解析的格式yyyy-MM-ddTHH:mm:ss)--不是空格了,是T

    LocalDateTime dateTime = LocalDateTime.parse("2019-01-14T12:04:12");
    int monthValue = dateTime.getMonthValue();
    int minute = dateTime.getMinute();
    System.out.println(monthValue+":"+minute);
    LocalTime time = LocalTime.parse("19:14:25");
    int hour = time.getHour();
    System.out.println(hour);
    LocalDate date = LocalDate.parse("2019-01-12");
    int year = date.getYear();
    System.out.println(year);
    

    14、总结

          jdk8中提供三个类(LocalDate,LocalTime,LocalDateTime),它们分别对年月日、时分秒和年月日时分秒进行单独的处理,使用时可以根据自己的实际情况进行选择,它们的API类似。

          Date与LocalDateTime、LocalDate、LocalTime互转参考  https://www.cnblogs.com/exmyth/p/6425878.html

  • 相关阅读:
    java内存溢出
    jstack命令使用
    JVM问题排查步骤
    c++指针常量和常量指针
    c++ 通讯录
    冒泡排序
    翻转数组
    敲桌子
    求一个100-999之间的水仙花数
    elasticsearch 模板的使用
  • 原文地址:https://www.cnblogs.com/lu51211314/p/10280739.html
Copyright © 2011-2022 走看看