LocalDateTime总结

前言

Instant——它代表的是时间戳
LocalDate——不包含具体时间的日期,比如2014-01-14。它可以用来存储生日,周年纪念日,入职日期等。
LocalTime——它代表的是不含日期的时间
LocalDateTime——它包含了日期及时间,不过还是没有偏移信息或者说时区。
ZonedDateTime——这是一个包含时区的完整的日期时间,偏移量是以UTC/格林威治时间为基准的。

LocalDateTime总结

 

public static void main(String[] args) {
    //------------------------------【获取当前时间】---------------------------------------------------
    LocalDateTime nowTime = LocalDateTime.now();
    //结果:2021-02-20T09:45:44.527
    System.out.println(nowTime);
    //------------------------------【获取年月日】-----------------------------------------------------
    //【方法1】
    String a = nowTime.format(DateTimeFormatter.ISO_DATE);
    //结果:2021-02-20
    System.out.println(a);
    //【方法2】
    String b = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    //结果:2021-02-20
    System.out.println(b);
    //------------------------------【获取时分秒】-----------------------------------------------------
    String c = nowTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
    //结果:09:45:44
    System.out.println(c);
    //------------------------------【获取年月日时分秒毫秒】--------------------------------------------
    String d = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS"));
    //结果:2021-02-20 09:45:44:527
    System.out.println(d);
    //------------------------------【获取秒数】-------------------------------------------------------
    Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
    //结果:1613785755
    System.out.println(second);
    //------------------------------【获取毫秒数】-----------------------------------------------------
    Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    //结果:1613785815633
    System.out.println(milliSecond);
    //------------------------------【毫秒转LocalDateTime】--------------------------------------------
    Long longTime = 1613786449976L;
    LocalDateTime ldt = Instant.ofEpochMilli(longTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
    //结果:2021-02-20T10:00:49.976
    System.out.println(ldt);
    //------------------------------【String转LocalDateTime】------------------------------------------
    String dateTimeStr = "2021-02-20 09:50:15";
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, df);
    //结果:2021-02-20T09:50:15
    System.out.println(dateTime);
    //------------------------------【LocalDateTime转String】------------------------------------------
    //除了下面的方法之外,上面类型转换的都可以看成是LocalDateTime转String  ZoneOffset.of("+8")意思为:默认时区为东8区
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String dateTime2 = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
    //结果:2021-02-20 09:55:52
    System.out.println(dateTime2);
    //------------------------------【LocalDateTime转Date】--------------------------------------------
    Date date = new Date();
    LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
    //结果:2021-02-20T09:59:14.719
    System.out.println(localDateTime);
    //------------------------------【Date转LocalDateTime】--------------------------------------------
    LocalDateTime localDateTime1 = LocalDateTime.now();
    Date date1 = Date.from(localDateTime1.toInstant(ZoneOffset.of("+8")));
    //结果:Sat Feb 20 10:00:49 CST 2021
    System.out.println(date1);
    //------------------------------【LocalDateTime获取年】--------------------------------------------
    System.out.println("当前时间为:"+LocalDateTime.now()+" == "+"2021-02-20T10:23:45.041");
    int year = LocalDateTime.now().getYear();
    //结果:2021
    System.out.println(year);
    //------------------------------【LocalDateTime获取月】--------------------------------------------
    Month month = LocalDateTime.now().getMonth();
    //结果:FEBRUARY
    System.out.println(month);
    int monthValue = LocalDateTime.now().getMonthValue();
    //结果:2
    System.out.println(monthValue);
    //------------------------------【LocalDateTime获取日】--------------------------------------------
    int dayOfMonth = LocalDateTime.now().getDayOfMonth();
    //结果:20
    System.out.println(dayOfMonth);
    DayOfWeek dayOfWeek = LocalDateTime.now().getDayOfWeek();
    //结果:SATURDAY
    System.out.println(dayOfWeek);
    int dayOfYear = LocalDateTime.now().getDayOfYear();
    //结果:51
    System.out.println(dayOfYear);
    //------------------------------【LocalDateTime获取时】--------------------------------------------
    int hour = LocalDateTime.now().getHour();
    //结果:10
    System.out.println(hour);
    //------------------------------【LocalDateTime获取分】--------------------------------------------
    int minute = LocalDateTime.now().getMinute();
    //结果:23
    System.out.println(minute);
    //------------------------------【LocalDateTime获取秒】--------------------------------------------
    int second1 = LocalDateTime.now().getSecond();
    //结果:45
    System.out.println(second1);
    //------------------------------【LocalDateTime获取特定日期】---------------------------------------
    LocalDateTime of = LocalDateTime.of(2021, 2, 20, 10, 37, 20);
    //结果:2021-02-20T10:37:20
    System.out.println(of);
    //------------------------------【LocalDateTime时间对比】-------------------------------------------
    LocalDateTime of1 = LocalDateTime.of(2021, 2, 20, 10, 37, 20);
    LocalDateTime of2 = LocalDateTime.of(2021, 2, 20, 10, 37, 22);
    LocalDateTime of3 = LocalDateTime.of(2021, 2, 20, 10, 37, 22);
    //【方法1】
    //结果:false
    System.out.println(of1.equals(of2));
    //结果:true
    System.out.println(of2.equals(of3));
    //结果:-1 of1 < of2
    System.out.println(of1.compareTo(of2));
    //结果:0 of2 = of3
    System.out.println(of2.compareTo(of3));
    //结果:1 of3 > of1
    System.out.println(of3.compareTo(of1));
    //【方法2】
    //结果:false
    System.out.println(of1.isAfter(of2));
    //结果:true
    System.out.println(of1.isBefore(of2));
    //结果:true
    System.out.println(of2.isEqual(of3));
    //------------------------------【LocalDateTime获取一天前/后日期】-----------------------------------
    LocalDateTime localDateTime2 = LocalDateTime.now().plusDays(1);
    //结果:2021-02-21T10:48:26.911
    System.out.println(localDateTime2);
    LocalDateTime localDateTime3 = LocalDateTime.now().plusDays(-1);
    //结果:2021-02-19T10:48:26.911
    System.out.println(localDateTime3);
    //------------------------------【LocalDateTime获取一小时前/后日期】-----------------------------------
    LocalDateTime localDateTime4 = LocalDateTime.now().plusHours(1);
    //结果:2021-02-20T11:49:32.320
    System.out.println(localDateTime4);
    LocalDateTime localDateTime5 = LocalDateTime.now().plusHours(-1);
    //结果:2021-02-20T09:49:32.320
    System.out.println(localDateTime5);
    //------------------------------【LocalDateTime获取一年前/后日期】-------------------------------------
    LocalDateTime localDateTime6 = LocalDateTime.now().plusYears(1);
    //结果:2022-02-20T10:51:47.585
    System.out.println(localDateTime6);
    LocalDateTime localDateTime7 = LocalDateTime.now().plusYears(-1);
    //结果:2020-02-20T10:51:47.585
    System.out.println(localDateTime7);
    //------------------------------【LocalDateTime获取一分钟前/后日期】-----------------------------------
    LocalDateTime localDateTime8 = LocalDateTime.now().plusMinutes(1);
    //结果:2021-02-20T13:54:39.209
    System.out.println(localDateTime8);
    LocalDateTime localDateTime9 = LocalDateTime.now().plusMinutes(-1);
    //结果:2021-02-20T13:52:39.209
    System.out.println(localDateTime9);
    //------------------------------【LocalDateTime获取一月前/后日期】-------------------------------------
    LocalDateTime localDateTime10 = LocalDateTime.now().plusMonths(1);
    //结果:2021-03-20T13:55:12.718
    System.out.println(localDateTime10);
    LocalDateTime localDateTime11 = LocalDateTime.now().plusMonths(-1);
    //结果:2021-01-20T13:55:12.718
    System.out.println(localDateTime11);
    //------------------------------【LocalDateTime获取一秒前/后日期】-------------------------------------
    LocalDateTime localDateTime12 = LocalDateTime.now().plusSeconds(1);
    //结果:2021-02-20T13:57:51.966
    System.out.println(localDateTime12);
    LocalDateTime localDateTime13 = LocalDateTime.now().plusSeconds(-1);
    //结果:2021-02-20T13:57:49.966
    System.out.println(localDateTime13);
    //------------------------------【LocalDateTime获取一周前/后日期】-------------------------------------
    LocalDateTime localDateTime14 = LocalDateTime.now().plusWeeks(1);
    //结果:2021-02-27T13:59:46.615
    System.out.println(localDateTime14);
    LocalDateTime localDateTime15 = LocalDateTime.now().plusWeeks(-1);
    //结果:2021-02-13T13:59:46.615
    System.out.println(localDateTime15);
}

 

参考:

https://www.cnblogs.com/comeboo/p/5378922.html

posted @ 2021-02-20 10:12  夏夜凉凉  阅读(870)  评论(0编辑  收藏  举报