LocalDateTime用法以及详细介绍

2019-10-10 15:08:47

摘自 LocalDateTime用法

前言

最近看别人项目源码,发现Java8新的日期时间API很方便强大,所以转载该入门介绍博客,记录一下。

使用新时间日期API的必要性

在java8以前,或许:

  • 当你在做有关时间日期的操作时,你会想到用Date;

  • 当你在做日期、月份、天数相加减时,你会想到用Calendar;

  • 当你需要对时间日期进行格式化时,你会想到使用SimpleDateFormat或DateFormat下的其他子类;
    ……

但是,你必须知道,以上有关的时间日期操作对象,都是可变的、线程不安全的,同时,如果作为一个经常写过类似代码的人来说,尽管有相关对象提供某些操作,但并不能很快、很简单的就能得到最终想要的结果,如:要计算两个时间点之间相差的年、月、日、周、时、分、秒等,这些计算尽管原有API也能够实现,但原有API除了线程不安全之外,另外一个不足之处就是代码繁琐,性能低!

为何我们总提多线程下,线程不安全?对于初学者来说,可能觉得能够简单实现出功能就已经足够,但是真正的开发项目是不可能仅仅考虑功能的实现的,还要考虑项目的安全性、稳定性、高性能、高可用性等等!因此,作为java开发者,多线程的知识是必不可少的。而也正因为多线程,才会出现一大堆问题(简称线程安全性问题),作为开发者,就应该写出不仅能实现功能的代码,还要是线程安全的代码。那么,学习并熟悉掌握新的线程安全的API就显得非常重要了!

没错,java8出的新的时间日期API都是线程安全的,并且性能更好,代码更简洁!

新时间日期API常用、重要对象介绍

  • ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则

  • Instant: 用来表示时间线上的一个点(瞬时)

  • LocalDate: 表示没有时区的日期, LocalDate是不可变并且线程安全的

  • LocalTime: 表示没有时区的时间, LocalTime是不可变并且线程安全的

  • LocalDateTime: 表示没有时区的日期时间, LocalDateTime是不可变并且线程安全的

  • Clock: 用于访问当前时刻、日期、时间,用到时区

  • Duration: 用秒和纳秒表示时间的数量(长短),用于计算两个日期的“时间”间隔

  • Period: 用于计算两个“日期”间隔

其中,LocalDate、LocalTime、LocalDateTime是新API里的基础对象,绝大多数操作都是围绕这几个对象来进行的,有必要搞清楚:

LocalDate : 只含年月日的日期对象
LocalTime :只含时分秒的时间对象
LocalDateTime : 同时含有年月日时分秒的日期对象

本文将以实例讲解日常开发中常用到的时间日期操作,如:

获取当前日期、时间
指定时间日期创建对应的对象
计算两个时间点的间隔
判断两个时间的前后
时间日期的格式化
获取时间戳
时间、日期相加减
获取给定时间点的年份、月份、周、星期等
……

新时间日期API详解与示例

获取当前时间
LocalDate localDate = LocalDate.now();LocalTime localTime = LocalTime.now();LocalDateTime localDateTime = LocalDateTime.now();System.out.println(localDate);System.out.println(localTime);System.out.println(localDateTime);

运行结果:


image.png

根据指定日期/时间创建对象
LocalDate localDate = LocalDate.of(2018, 1, 13);LocalTime localTime = LocalTime.of(9, 43, 20);LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 13, 9, 43, 20);System.out.println(localDate);System.out.println(localTime);System.out.println(localDateTime);

运行结果:


image.png

日期时间的加减
  • 对于LocalDate,只有精度大于或等于日的加减,如年、月、日;

  • 对于LocalTime,只有精度小于或等于时的加减,如时、分、秒、纳秒;

  • 对于LocalDateTime,则可以进行任意精度的时间相加减;

LocalDateTime localDateTime = LocalDateTime.now();//以下方法的参数都是long型,返回值都是LocalDateTimeLocalDateTime plusYearsResult = localDateTime.plusYears(2L);LocalDateTime plusMonthsResult = localDateTime.plusMonths(3L);LocalDateTime plusDaysResult = localDateTime.plusDays(7L);LocalDateTime plusHoursResult = localDateTime.plusHours(2L);LocalDateTime plusMinutesResult = localDateTime.plusMinutes(10L);LocalDateTime plusSecondsResult = localDateTime.plusSeconds(10L);
        System.out.println("当前时间是 : " + localDateTime + "\n"
        + "当前时间加2年后为 : " + plusYearsResult + "\n"
        + "当前时间加3个月后为 : " + plusMonthsResult + "\n"
        + "当前时间加7日后为 : " + plusDaysResult + "\n"
        + "当前时间加2小时后为 : " + plusHoursResult + "\n"
        + "当前时间加10分钟后为 : " + plusMinutesResult + "\n"
        + "当前时间加10秒后为 : " + plusSecondsResult + "\n"
        );
    //也可以以另一种方式来相加减日期,即plus(long amountToAdd, TemporalUnit unit)//                  参数1 : 相加的数量, 参数2 : 相加的单位LocalDateTime nextMonth = localDateTime.plus(1, ChronoUnit.MONTHS);LocalDateTime nextYear = localDateTime.plus(1, ChronoUnit.YEARS);LocalDateTime nextWeek = localDateTime.plus(1, ChronoUnit.WEEKS);
        System.out.println("now : " + localDateTime + "\n"
        + "nextYear : " + nextYear + "\n"
        + "nextMonth : " + nextMonth + "\n"
        + "nextWeek :" + nextWeek + "\n"
        );
        //日期的减法用法一样,在此不再举例

运行结果:


image.png

将年、月、日等修改为指定的值,并返回新的日期(时间)对象

析: 其效果与时间日期相加减差不多,如今天是2018-01-13,要想变为2018-01-20有两种方式
a. localDate.plusDays(20L) -> 相加指定的天数
b. localDate.withDayOfYear(20) -> 直接指定到哪一天

LocalDate localDate = LocalDate.now();//当前时间基础上,指定本年当中的第几天,取值范围为1-365,366LocalDate withDayOfYearResult = localDate.withDayOfYear(200);//当前时间基础上,指定本月当中的第几天,取值范围为1-29,30,31LocalDate withDayOfMonthResult = localDate.withDayOfMonth(5);//当前时间基础上,直接指定年份LocalDate withYearResult = localDate.withYear(2017);//当前时间基础上,直接指定月份LocalDate withMonthResult = localDate.withMonth(5);System.out.println("当前时间是 : " + localDate + "\n"
        + "指定本年当中的第200天 : " + withDayOfYearResult + "\n"
        + "指定本月当中的第5天 : " + withDayOfMonthResult + "\n"
        + "直接指定年份为2017 : " + withYearResult + "\n"
        + "直接指定月份为5月 : " + withMonthResult + "\n"
        );

运行结果:


image.png

获取日期的年月日周时分秒
LocalDateTime localDateTime = LocalDateTime.now();int dayOfYear = localDateTime.getDayOfYear();int dayOfMonth = localDateTime.getDayOfMonth();DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();System.out.println("今天是" + localDateTime + "\n"
        + "本年当中第" + dayOfYear + "天" + "\n"
        + "本月当中第" + dayOfMonth + "天" + "\n"
        + "本周中星期" + dayOfWeek.getValue() + "-即" + dayOfWeek + "\n");
        //获取当天时间的年月日时分秒int year = localDateTime.getYear();Month month = localDateTime.getMonth();int day = localDateTime.getDayOfMonth();int hour = localDateTime.getHour();int minute = localDateTime.getMinute();int second = localDateTime.getSecond();System.out.println("今天是" + localDateTime + "\n"
        + "年 : " + year + "\n"
        + "月 : " + month.getValue() + "-即 "+ month + "\n"
        + "日 : " + day + "\n"
        + "时 : " + hour + "\n"
        + "分 : " + minute + "\n"
        + "秒 : " + second + "\n"
        );

运行结果:

image.png

时间日期前后的比较与判断
//判断两个时间点的前后LocalDate localDate1 = LocalDate.of(2017, 8, 8);LocalDate localDate2 = LocalDate.of(2018, 8, 8);boolean date1IsBeforeDate2 = localDate1.isBefore(localDate2);System.out.println("date1IsBeforeDate2 : " + date1IsBeforeDate2);// date1IsBeforeDate2 == true
判断是否为闰年
LocalDate now = LocalDate.now();System.out.println("now : " + now + ", is leap year ? " + );
java8时钟 : clock()
//返回当前时间,根据系统时间和UTCClock clock = Clock.systemUTC();// 运行结果: SystemClock[Z]System.out.println(clock);
时间戳

事实上Instant就是java8以前的Date,
可以使用以下两个类中的方法在这两个类型之间进行转换,
比如Date.from(Instant)就是用来把Instant转换成java.util.date的,
而new Date().toInstant()就是将Date转换成Instant的

Instant instant = Instant.now();//2019-06-08T16:50:19.174ZSystem.out.println(instant);Date date = Date.from(instant);Instant instant2 = date.toInstant();//Sun Jun 09 00:50:19 CST 2019System.out.println(date);//2019-06-08T16:50:19.174ZSystem.out.println(instant2);
计算时间、日期间隔

Duration:用于计算两个“时间”间隔
Period:用于计算两个“日期”间隔

//计算两个日期的日期间隔-年月日LocalDate date1 = LocalDate.of(2018, 2, 13);LocalDate date2 = LocalDate.of(2017, 3, 12);//内部是用date2-date1,所以得到的结果是负数Period period = Period.between(date1, date2);System.out.println("相差年数 : " + period.getYears());System.out.println("相差月数 : " + period.getMonths());System.out.println("相差日数 : " + period.getDays());//还可以这样获取相差的年月日System.out.println("-------------------------------");long years = period.get(ChronoUnit.YEARS);long months = period.get(ChronoUnit.MONTHS);long days = period.get(ChronoUnit.DAYS);System.out.println("相差的年月日分别为 : " + years + "," + months + "," + days);//注意,当获取两个日期的间隔时,并不是单纯的年月日对应的数字相加减,而是会先算出具体差多少天,在折算成相差几年几月几日的
        //计算两个时间的间隔System.out.println("-------------------------------");LocalDateTime date3 = LocalDateTime.now();LocalDateTime date4 = LocalDateTime.of(2018, 1, 13, 22, 30, 10);Duration duration = Duration.between(date3, date4);System.out.println(date3 + " 与 " + date4 + " 间隔  " + "\n"
        + " 天 :" + duration.toDays() + "\n"
        + " 时 :" + duration.toHours() + "\n"
        + " 分 :" + duration.toMinutes() + "\n"
        + " 毫秒 :" + duration.toMillis() + "\n"
        + " 纳秒 :" + duration.toNanos() + "\n"
        );//注意,并没有获得秒差的,但既然可以获得毫秒,秒就可以自行获取了

运行结果:


image.png

当计算程序的运行时间时,应当使用时间戳Instant

Instant ins1 = Instant.now();for (int i = 0; i < 10000000; i++) {
            //循环一百万次}Instant ins2 = Instant.now();Duration duration = Duration.between(ins1, ins2);System.out.println("程序运行耗时为 : " + duration.toMillis() + "毫秒");
时间日期的格式化(格式化后返回的类型是String)
1. 使用jdk自身配置好的日期格式
//使用jdk自身配置好的日期格式DateTimeFormatter formatter1 = DateTimeFormatter.ISO_DATE_TIME;LocalDateTime date1 = LocalDateTime.now();//反过来调用也可以 : date1.format(formatter1);String date1Str = formatter1.format(date1);System.out.println(date1Str);

运行结果:


image.png

2. 使用自定义格式
LocalDateTime date1 = LocalDateTime.now();DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");String date2Str = formatter2.format(date1);System.out.println(date2Str);

运行结果:


image.png

注:自定义转化的格式一定要与日期类型对应

  • LocalDate只能设置仅含年月日的格式

  • LocalTime只能设置仅含时分秒的格式

  • LocalDateTime可以设置含年月日时分秒的格式

代码如下:

DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd");System.out.println(formatter3.format(LocalDate.now()));
        System.out.println("-------------------------------");
        DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern("HH:mm:ss");System.out.println(formatter4.format(LocalTime.now()));

运行结果:


image.png

将时间字符串形式转化为日期对象
String datetime =  "2018-01-13 21:27:30";  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime ldt = LocalDateTime.parse(datetime, dtf);  System.out.println(ldt);

运行结果:


image.png

注:格式的写法必须与字符串的形式一样
2018-01-13 21:27:30 对应 yyyy-MM-dd HH:mm:ss
20180113213328 对应 yyyyMMddHHmmss
否则会报运行时异常!

但要记住:得到的最终结果都是类似2018-01-13T21:27:30的格式

因为在输出LocalDateTime对象时,会调用其重写的toString方法。

@Override
    public String toString() {
        return date.toString() + 'T' + time.toString();
    }
将时间日期对象转为格式化后的时间日期对象
//新的格式化API中,格式化后的结果都默认是String,有时我们也需要返回经过格式化的同类型对象LocalDateTime ldt1 = LocalDateTime.now();DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String temp = dtf1.format(ldt1);LocalDateTime formatedDateTime = LocalDateTime.parse(temp, dtf1);System.out.println(formatedDateTime);

运行结果:

image.png

long毫秒值转换为日期
System.out.println("---------long毫秒值转换为日期---------");DateTimeFormatter df= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String longToDateTime = df.format(LocalDateTime.ofInstant(
   Instant.ofEpochMilli(System.currentTimeMillis()),ZoneId.of("Asia/Shanghai")));System.out.println(longToDateTime);

运行结果:


image.png


  • 2021-01-13 13:36:29

    shortid nodejs短id生成器

    短ID在实际运用中很广泛, 其中比较典型的运用就是短地址。 市面上肯定有不少开源的生成短ID库, 基于node.js的估计也不少。 鉴于本人已然是node.js的脑残粉(本职java开发), 很多业余项目从前端到后端都基于javascript开发, 加上npm和bower的包管理以及grunt的打包工具, 在项目开发过程中体验特别酸爽。 由于当时项目前后端都会用到短ID, 但没找到合适的库同时支持npm和bower的(可能孤陋寡闻). 因此自己乐此不疲地又造了个轮子js-shortid(夷,为什么会说又呢?!). 下面主要介绍它的实现方案, 自认为比较优雅简洁。

  • 2021-01-13 17:23:21

    CREATE TABLE 表名 AS SELECT 语句 快速复制表但是锁表

    注意Table2的主键约束,如果Table2有主键而且不为空,则 field1, field2…中必须包括主键 在执行语句的时候,MySQL是逐行加锁的(扫描一个锁一个),直至锁住所有符合条件的数据,执行完毕才释放锁。所以当业务在进行的时候,切忌使用这种方法。 在RR隔离级别下,还会加行锁和间隙锁

  • 2021-01-13 17:27:04

    Navicat配置mysql数据库用户权限

    用数据库的时候就会遇到有多个用户,分配用户权限的情况,有些用户只读,有些用户可以读写,有些用户只能操作一个或者多个数据库,如何给mysql的用户设置权限,我这里描述一下如何用navicat图形操作分配用户权限

  • 2021-01-14 06:15:04

    通过glide获取图片显示后的真正宽高

    有时候需要获取网络图片的宽高来设置图片显示的大小,很多人会直接利用Glide的加载监听去拿图片的宽高,但是这样拿到的不是图片真正的宽高,而是图片显示在ImageView后的宽高。如下:

  • 2021-01-14 09:38:57

    Chrome插件详细教程

    严格来讲,我们正在说的东西应该叫Chrome扩展(Chrome Extension),真正意义上的Chrome插件是更底层的浏览器功能扩展,可能需要对浏览器源码有一定掌握才有能力去开发。鉴于Chrome插件的叫法已经习惯,本文也全部采用这种叫法,但读者需深知本文所描述的Chrome插件实际上指的是Chrome扩展。

  • 2021-01-14 17:07:51

    chrome.contextMenus.create不出现菜单

    主要原因是,我每次刷新玩,都复制一下右键,然而并没有出现菜单,一度颓废啊,因为demo,还有其他人的文章都是这样的。 哎,后来发现是这样的,我缺少了contexts选项。其实我是成功了,我现在只要不选择文字,直接点右键,菜单已经出现了哦。