当前位置:主页 > java教程 > Java LocalDateTime

Java中LocalDateTime的具体用法

发布:2023-03-12 08:00:01 59


为网友们分享了相关的编程文章,网友逯弘化根据主题投稿了本篇教程内容,涉及到Java LocalDateTime、Java LocalDateTime相关内容,已被861网友关注,相关难点技巧可以阅读下方的电子资料。

Java LocalDateTime

一.背景

本文主要介绍Java 8中时间的操作方法

  • java.util.Date是用于表示一个日期和时间的对象(注意与java.sql.Date区分,后者用在数据库中没有格式化的Date),它打印出的日期可读性差,可以使用SimpleDateFormat对时间进行格式化,但SimpleDateFormat又是线程不安全,包括format和parse方法,而在时间的计算方面不是很方便。
  • java.util.Canlendar 可以用于获取并设置年、月、日、时、分、秒,它和Date比,主要多了一个可以做简单的日期和时间运算的功能,Canlendar 变量是全局变量,会导致脏变量情况产生,并且这个共享变量没有做线程安全控制,也就是多线程的情况下是线程不安全的。
  • Java8出的新的时间日期API都是线程安全的比如LocalDate、LocalTime、LocalDateTime这三个类,计算功能强大,并且性能更好,代码更简洁。
  • 进行相关实例时,请先确保已安装JDK8,本文采用的版本是:jdk1.8.0_111

二.简介 

  • LocalDate :表示当前日期,相当于:yyyy-MM-dd
  • LocalTime :表示当前时间,相当于:HH:mm:ss (24小时制) 或者 hh:mm:ss(12小时制)
  • LocalDateTime :表示当前日期时间,相当于:yyyy-MM-ddTHH:mm:ss ,是前两者的结合
  • DateTimeFormatter :表示格式化类型,可以取代SimpleDateFormat
  • Instant :表示时刻,用来表示时间线上的一个点(瞬时),也可以说是时间戳
  • ZoneId、ZoneOffset :表示时区
  • ZonedDateTime :表示带时区的日期和时间,是前两者的结合
  • Duration :表示两个时刻之间的时间间隔
  • Period :表示两个日期之间的天数

三.实战

3.1 LocalDate的创建与使用

3.1.1、LocalDate的创建

    /**
     * LocalDate的初始化方法
     * 此处单元测试的注解是采用:org.junit.Test
     */
    @Test
    public void init(){
        //1.创建LocalDate,LocalDate对象直接调用now(),获取到当前日期
        LocalDate localDateNow = LocalDate.now();
        System.out.println("1.直接调用now()创建:" + localDateNow);
        //2.直接根据(年月日)创建,如:2021-05-20
        LocalDate localDateOf = LocalDate.of(2021, 5, 20);
        System.out.println("2.根据年月日创建:" + localDateOf);
        //3.直接根据某一年的某一天创建,如 2021年中第200天
        LocalDate localDateOfYearDay = LocalDate.ofYearDay(2021, 200);
        System.out.println("3.根据某一年的某一天创建:" + localDateOfYearDay);
        //4.根据java.time.temporal.TemporalAccessor创建(包括LocalDate,LocalDateTime等等)
        LocalDate localDateFrom = LocalDate.from(LocalDate.now());
        System.out.println("4.根据TemporalAccessor创建:" + localDateFrom);
        //5.根据时间字符串转化创建,调用parse(CharSequence text)方法,此方法只支持yyyy-MM-dd格式的值
        LocalDate localDateParse = LocalDate.parse("2021-05-11");
        System.out.println("5.根据时间字符串转化创建:" + localDateParse);
        //6.根据时间字符串转化创建,调用parse(CharSequence text, DateTimeFormatter formatter)方法,此时的text可以根据formatter多变
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate localDateParse2 = LocalDate.parse("20210515", formatter);
        System.out.println("6.根据时间字符串转化创建:" + localDateParse2);
    }

运行结果:

1.直接调用now()创建:2021-05-24
2.根据年月日创建:2021-05-20
3.根据某一年的某一天创建:2021-07-19
4.根据TemporalAccessor创建:2021-05-24
5.根据时间字符串转化创建:2021-05-11
6.根据时间字符串转化创建:2021-05-15

3.1.2、LocalDate的常见使用方法

   /**
     * LocalDate的常用使用方法
     * 此处单元测试的注解是采用:org.junit.Test
     */
    @Test
    public void usage() {
        //此处采用LocalDate对象直接调用now(),获取到当前日期,注意:如果使用我的实例,结果会不一样,因为LocalDate.now()是调用时的日期
        LocalDate currentDate = LocalDate.now();
        //1.获取年、月、日、星期几、月中天、年中天、月数
        System.out.println("------------------1.获取年、月、日、星期几、月中天、年中天、月数-----------------------");
        System.out.println("1.获取到的当前日期:" + currentDate);
        System.out.println("1.获取到的年:" + currentDate.getYear());
        System.out.println("1.获取到的月:" + currentDate.getMonthValue());
        System.out.println("1.获取到的一月中的哪一天:" + currentDate.getDayOfMonth());
        System.out.println("1.获取到的一周中的星期几:" + currentDate.getDayOfWeek().getValue());
        System.out.println("1.获取到的一年的第多少天:" + currentDate.getDayOfYear());
        System.out.println("1.获取到的一年有多少天:" + currentDate.lengthOfYear());
        System.out.println("1.获取到的一年有多少月:" + currentDate.lengthOfMonth());
        //2.时间的计算
        System.out.println("-----------------2.时间的计算,主要是加减法------------------------");
        System.out.println("2.获取到的当前日期:" + currentDate);
        System.out.println("2.加法,当前日期上加2年:" + currentDate.plusYears(2));
        System.out.println("2.加法,当前日期上加3个月:" + currentDate.plusMonths(3));
        System.out.println("2.加法,当前日期上加20天:" + currentDate.plusDays(20));
        System.out.println("2.加法,当前日期上加一周:" + currentDate.plusWeeks(1));
        System.out.println("2.减法,当前日期上减2年:" + currentDate.minusYears(2));
        System.out.println("2.减法,当前日期上减3个月:" + currentDate.minusMonths(3));
        System.out.println("2.减法,当前日期上减20天:" + currentDate.minusDays(20));
        System.out.println("2.减法,当前日期上减一周:" + currentDate.minusWeeks(1));
        //3.时间的判断及转化
        System.out.println("-----------------3.时间的判断及格式化(格式化的类型很多)------------------------");
        //新建一个格式化类型
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        //新创建一个LocalDate日期进行比较
        LocalDate localDateOf = LocalDate.of(2020, 5, 20);
        System.out.println("3.当前日期转成yyyyMMdd型的字符串:" + currentDate.format(formatter));
        System.out.println("3.当前日期是否在一个日期之后:" + currentDate.isAfter(localDateOf));
        System.out.println("3.当前日期是否在一个日期之前:" + currentDate.isBefore(localDateOf));
        System.out.println("3.当前日期是否是闰年:" + currentDate.isLeapYear());
        System.out.println("3.2020-05-20是否是闰年:" + localDateOf.isLeapYear());
        //4.根据指定数据获取日期或者时间(LocalTime)
        System.out.println("-----------------4.根据指定数据获取日期或者时间(LocalTime)------------------------");
        System.out.println("4.获取到的当前日期:" + currentDate);
        System.out.println("4.修改年数为1999年:" + currentDate.withYear(1999));
        System.out.println("4.修改月数为10月:" + currentDate.withMonth(10));
        System.out.println("4.修改天数为当月12日:" + currentDate.withDayOfMonth(12));
        System.out.println("4.获取到的当前日期的开始时间:" + currentDate.atStartOfDay());
        System.out.println("4.根据指定的时、分、秒获取时间:" + currentDate.atTime(12, 23, 45));
        System.out.println("4.根据时间LocalTime对象获取时间:" + currentDate.atTime(LocalTime.now()));
    }

运行结果:

------------------1.获取年、月、日、星期几、月中天、年中天、月数-----------------------
1.获取到的当前日期:2021-05-24
1.获取到的年:2021
1.获取到的月:5
1.获取到的一月中的哪一天:24
1.获取到的一周中的星期几:1
1.获取到的一年的第多少天:144
1.获取到的一年有多少天:365
1.获取到的一年有多少月:31
-----------------2.时间的计算,主要是加减法------------------------
2.获取到的当前日期:2021-05-24
2.加法,当前日期上加2年:2023-05-24
2.加法,当前日期上加3个月:2021-08-24
2.加法,当前日期上加20天:2021-06-13
2.加法,当前日期上加一周:2021-05-31
2.减法,当前日期上减2年:2019-05-24
2.减法,当前日期上减3个月:2021-02-24
2.减法,当前日期上减20天:2021-05-04
2.减法,当前日期上减一周:2021-05-17
-----------------3.时间的判断及格式化(格式化的类型很多)------------------------
3.当前日期转成yyyyMMdd型的字符串:20210524
3.当前日期是否在一个日期之后:true
3.当前日期是否在一个日期之前:false
3.当前日期是否是闰年:false
3.2020-05-20是否是闰年:true
-----------------4.根据指定数据获取日期或者时间(LocalTime)------------------------
4.获取到的当前日期:2021-05-24
4.修改年数为1999年:1999-05-24
4.修改月数为10月:2021-10-24
4.修改天数为当月12日:2021-05-12
4.获取到的当前日期的开始时间:2021-05-24T00:00
4.根据指定的时、分、秒获取时间:2021-05-24T12:23:45
4.根据时间LocalTime对象获取时间:2021-05-24T19:49:04.468

3.2 LocalTime的创建与使用

3.2.1、LocalTime创建

    /**
     * LocalTime的初始化方法
     * 此处单元测试的注解是采用:org.junit.Test
     */
    @Test
    public void init() {
        //1.LocalTime,LocalTime对象直接调用now(),获取到当前时间
        LocalTime now = LocalTime.now();
        System.out.println("1.获取到的当前时间:" + now);
        //2.根据指定的时分秒生成时间
        LocalTime localTimeOf = LocalTime.of(23, 59, 59);
        System.out.println("2.根据指定的时分秒生成时间:" + localTimeOf);
        //3.根据指定的时分秒生成时间
        LocalTime localTimeParse = LocalTime.parse("12:23:45");
        System.out.println("3.根据指定的时分秒生成时间:" + localTimeParse);
        //4.根据指定的时分秒和格式化类型生成时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmmss");//注意:HH是24小时制,hh是12小时制,时间类型的不要出现年月日如:yyyyMMdd
        LocalTime localTimeParseWithFormatter = LocalTime.parse("102008", formatter);
        System.out.println("4.根据指定的时分秒和格式化类型生成时间:" + localTimeParseWithFormatter);
        //5.根据时间对象TemporalAccessor生成
        LocalTime initTime = LocalTime.of(11, 59, 59);
        System.out.println("5.根据时间对象TemporalAccessor生成:" + LocalTime.from(initTime));
    }

运行结果:

1.获取到的当前时间:11:36:05.740
2.根据指定的时分秒生成时间:23:59:59
3.根据指定的时分秒生成时间:12:23:45
4.根据指定的时分秒和格式化类型生成时间:10:20:08
5.根据时间对象TemporalAccessor生成:11:59:59

3.2.2、LocalTime的常见使用方法

/**
     * LocalTime的常用使用方法
     * 此处单元测试的注解是采用:org.junit.Test
     * 和LocalDate的操作方法差不多
     */
    @Test
    public void usage() {
        //此处采用LocalTime对象直接调用now(),获取到当前时间,注意:如果使用我的实例,结果会不一样,因为LocalTime.now()是调用时的时间
        LocalTime currentTime = LocalTime.now();
        //1.获取时、分、秒
        System.out.println("------------------1.获取时、分、秒-----------------------");
        System.out.println("1.获取到的当前时间:" + currentTime);
        System.out.println("1.获取当前时间的小时:" + currentTime.getHour());
        System.out.println("1.获取当前时间的分钟:" + currentTime.getMinute());
        System.out.println("1.获取当前时间的秒:" + currentTime.getSecond());
        System.out.println("1.获取当前时间的秒:" + currentTime.getNano());
        //2.时间的加减
        System.out.println("------------------2.时间的加减-----------------------");
        System.out.println("2.获取到的当前时间:" + currentTime);
        System.out.println("2.加法:当前时间上增加1小时" + currentTime.plusHours(1));
        System.out.println("2.加法:当前时间上增加10分钟" + currentTime.plusMinutes(10));
        System.out.println("2.加法:当前时间上增加20秒" + currentTime.plusSeconds(20));
        System.out.println("2.减法:当前时间上减加2小时" + currentTime.minusHours(2));
        System.out.println("2.减法:当前时间上减加30分钟" + currentTime.minusMinutes(30));
        System.out.println("2.减法:当前时间上减加5秒" + currentTime.minusSeconds(5));
        System.out.println("------------------3.时间的判断及转化-----------------------");
        //初始化一个新的时间
        LocalTime initTime = LocalTime.of(13, 59, 59);
        System.out.println("3.获取到的当前时间:" + currentTime);
        System.out.println("3.新初始化的时间:" + initTime);
        System.out.println("3.判断当前时间是否是一个时间之后:" + currentTime.isAfter(initTime));
        System.out.println("3.判断当前时间是否是一个时间之前:" + currentTime.isBefore(initTime));
        System.out.println("3.修改小时数为12:" + currentTime.withHour(12));
        System.out.println("3.修改分钟数为12:" + currentTime.withMinute(12));
        System.out.println("3.修改秒数为12:" + currentTime.withSecond(12));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime parseTime = LocalTime.parse("11:45:28", formatter);
        System.out.println("3.根据指定的时分秒和格式化类型生成时间:" + parseTime);
    }

运行结果:

------------------1.获取时、分、秒-----------------------
1.获取到的当前时间:19:50:14.612
1.获取当前时间的小时:19
1.获取当前时间的分钟:50
1.获取当前时间的秒:14
1.获取当前时间的秒:612000000
------------------2.时间的加减-----------------------
2.获取到的当前时间:19:50:14.612
2.加法:当前时间上增加1小时20:50:14.612
2.加法:当前时间上增加10分钟20:00:14.612
2.加法:当前时间上增加20秒19:50:34.612
2.减法:当前时间上减加2小时17:50:14.612
2.减法:当前时间上减加30分钟19:20:14.612
2.减法:当前时间上减加5秒19:50:09.612
------------------3.时间的判断及转化-----------------------
3.获取到的当前时间:19:50:14.612
3.新初始化的时间:13:59:59
3.判断当前时间是否是一个时间之后:true
3.判断当前时间是否是一个时间之前:false
3.修改小时数为12:12:50:14.612
3.修改分钟数为12:19:12:14.612
3.修改秒数为12:19:50:12.612
3.根据指定的时分秒和格式化类型生成时间:11:45:28

3.3 LocalDateTime的创建与使用

3.3.1、LocalDateTime创建

/**
     * LocalDateTime的初始化方法
     * 此处单元测试的注解是采用:org.junit.Test
     */
    @Test
    public void init() {
        //1.LocalDateTime对象直接调用now(),获取到当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("1.LocalDateTime对象直接调用now()获取到的时间:" + localDateTime);
        //2.根据年月日时分秒构造(此处方法比较多,不一一介绍)
        LocalDateTime localDateTimeOf = LocalDateTime.of(2021, 5, 10, 18, 30, 26);
        System.out.println("2.根据年月日时分秒构造获取到的时间:" + localDateTimeOf);
        //3.根据LocalDate和LocalTime得到(在有日期和时间的情况下可以使用)
        LocalDateTime of = LocalDateTime.of(LocalDate.now(), LocalTime.now());
        System.out.println("3.根据LocalDate和LocalTime得到:" + of);
        //4.LocalDate指定一个LocalTime(LocalDate只有年月日)
        LocalTime localTimeInit = LocalTime.of(14, 25, 25);
        LocalDateTime localDateWithLocalTime = LocalDate.now().atTime(localTimeInit);
        System.out.println("4.LocalDate指定一个LocalTime:" + localDateWithLocalTime);
        //5.LocalTime指定一个LocalDate(LocalTime只有时分秒)
        LocalDate localDateInit = LocalDate.of(1998, 10, 1);
        LocalDateTime localTimeWithLocalDate = LocalTime.now().atDate(localDateInit);
        System.out.println("5.LocalTime指定一个LocalDate:" + localTimeWithLocalDate);
    }

运行结果:

1.LocalDateTime对象直接调用now()获取到的时间:2021-05-24T19:51:15.237
2.根据年月日时分秒构造获取到的时间:2021-05-10T18:30:26
3.根据LocalDate和LocalTime得到:2021-05-24T19:51:15.238
4.LocalDate指定一个LocalTime:2021-05-24T14:25:25
5.LocalTime指定一个LocalDate:1998-10-01T19:51:15.238

3.3.2、LocalDateTime的常见使用方法

    /**
     * LocalDateTime的常用使用方法
     * 此处单元测试的注解是采用:org.junit.Test
     */
    @Test
    public void usage() {
        //1.根据LocalDateTime获取LocalDate
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println("1.根据LocalDateTime获取LocalDate: " + currentTime.toLocalDate());
        //2.根据LocalDateTime获取LocalTime
        System.out.println("2.根据LocalDateTime获取LocalTime: " + currentTime.toLocalTime());
        System.out.println("------------------3.时间的加减法及修改-----------------------");
        //3.LocalDateTime的加减法包含了LocalDate和LocalTime的所有加减,上面说过,这里就只做简单介绍
        System.out.println("3.当前时间:" + currentTime);
        System.out.println("3.当前时间加5年:" + currentTime.plusYears(5));
        System.out.println("3.当前时间加2个月:" + currentTime.plusMonths(2));
        System.out.println("3.当前时间减2天:" + currentTime.minusDays(2));
        System.out.println("3.当前时间减5个小时:" + currentTime.minusHours(5));
        System.out.println("3.当前时间加5分钟:" + currentTime.plusMinutes(5));
        System.out.println("3.当前时间加20秒:" + currentTime.plusSeconds(20));
        //还可以灵活运用比如:向后加一年,向前减一天,向后加2个小时,向前减5分钟,可以进行连写
        System.out.println("3.同时修改(向后加一年,向前减一天,向后加2个小时,向前减5分钟):" + currentTime.plusYears(1).minusDays(1).plusHours(2).minusMinutes(5));
        System.out.println("3.修改年为2025年:" + currentTime.withYear(2025));
        System.out.println("3.修改月为12月:" + currentTime.withMonth(12));
        System.out.println("3.修改日为27日:" + currentTime.withDayOfMonth(27));
        System.out.println("3.修改小时为12:" + currentTime.withHour(12));
        System.out.println("3.修改分钟为12:" + currentTime.withMinute(12));
        System.out.println("3.修改秒为12:" + currentTime.withSecond(12));
        System.out.println("------------------4.时间的转化及其他-----------------------");
        //4.时间的格式化及其他
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse("2020-09-18 14:55:44", formatter);
        System.out.println("4.时间字符串转为时间:" + parse);
        LocalDate localDate = LocalDate.now();
        System.out.println("4.所属年份的第一天:" + localDate.with(firstDayOfYear()));
        System.out.println("4.所属年份的最后一天:" + localDate.with(lastDayOfYear()));
        System.out.println("4.所属年份的下一年的第一天:" + localDate.with(firstDayOfNextYear()));
        System.out.println("4.所属月份的第一天:" + localDate.with(firstDayOfMonth()));
        System.out.println("4.所属月份的最后一天:" + localDate.with(lastDayOfMonth()));
        System.out.println("4.所属月份的下个月的第一天:" + localDate.with(firstDayOfNextMonth()));
    }

运行结果:

1.根据LocalDateTime获取LocalDate: 2021-05-24
2.根据LocalDateTime获取LocalTime: 19:57:46.316
------------------3.时间的加减法及修改-----------------------
3.当前时间:2021-05-24T19:57:46.316
3.当前时间加5年:2026-05-24T19:57:46.316
3.当前时间加2个月:2021-07-24T19:57:46.316
3.当前时间减2天:2021-05-22T19:57:46.316
3.当前时间减5个小时:2021-05-24T14:57:46.316
3.当前时间加5分钟:2021-05-24T20:02:46.316
3.当前时间加20秒:2021-05-24T19:58:06.316
3.同时修改(向后加一年,向前减一天,向后加2个小时,向前减5分钟):2022-05-23T21:52:46.316
3.修改年为2025年:2025-05-24T19:57:46.316
3.修改月为12月:2021-12-24T19:57:46.316
3.修改日为27日:2021-05-27T19:57:46.316
3.修改小时为12:2021-05-24T12:57:46.316
3.修改分钟为12:2021-05-24T19:12:46.316
3.修改秒为12:2021-05-24T19:57:12.316
------------------4.时间的转化及其他-----------------------
4.时间字符串转为时间:2020-09-18T14:55:44
4.所属年份的第一天:2021-01-01
4.所属年份的最后一天:2021-12-31
4.所属年份的下一年的第一天:2022-01-01
4.所属月份的第一天:2021-05-01
4.所属月份的最后一天:2021-05-31
4.所属月份的下个月的第一天:2021-06-01

到此这篇关于Java中LocalDateTime的具体用法的文章就介绍到这了,更多相关Java LocalDateTime内容请搜索码农之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持码农之家!


参考资料

相关文章

网友讨论