最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中LocalDateTime的具體用法

 更新時(shí)間:2023年01月15日 09:41:33   作者:遨游在知識(shí)的海洋里無法自拔  
本文主要介紹了Java中LocalDateTime的具體用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一.背景

本文主要介紹Java 8中時(shí)間的操作方法

  • java.util.Date是用于表示一個(gè)日期和時(shí)間的對(duì)象(注意與java.sql.Date區(qū)分,后者用在數(shù)據(jù)庫中沒有格式化的Date),它打印出的日期可讀性差,可以使用SimpleDateFormat對(duì)時(shí)間進(jìn)行格式化,但SimpleDateFormat又是線程不安全,包括format和parse方法,而在時(shí)間的計(jì)算方面不是很方便。
  • java.util.Canlendar 可以用于獲取并設(shè)置年、月、日、時(shí)、分、秒,它和Date比,主要多了一個(gè)可以做簡單的日期和時(shí)間運(yùn)算的功能,Canlendar 變量是全局變量,會(huì)導(dǎo)致臟變量情況產(chǎn)生,并且這個(gè)共享變量沒有做線程安全控制,也就是多線程的情況下是線程不安全的。
  • Java8出的新的時(shí)間日期API都是線程安全的比如LocalDate、LocalTime、LocalDateTime這三個(gè)類,計(jì)算功能強(qiáng)大,并且性能更好,代碼更簡潔。
  • 進(jìn)行相關(guān)實(shí)例時(shí),請(qǐng)先確保已安裝JDK8,本文采用的版本是:jdk1.8.0_111

二.簡介 

  • LocalDate :表示當(dāng)前日期,相當(dāng)于:yyyy-MM-dd
  • LocalTime :表示當(dāng)前時(shí)間,相當(dāng)于:HH:mm:ss (24小時(shí)制) 或者 hh:mm:ss(12小時(shí)制)
  • LocalDateTime :表示當(dāng)前日期時(shí)間,相當(dāng)于:yyyy-MM-ddTHH:mm:ss ,是前兩者的結(jié)合
  • DateTimeFormatter :表示格式化類型,可以取代SimpleDateFormat
  • Instant :表示時(shí)刻,用來表示時(shí)間線上的一個(gè)點(diǎn)(瞬時(shí)),也可以說是時(shí)間戳
  • ZoneId、ZoneOffset :表示時(shí)區(qū)
  • ZonedDateTime :表示帶時(shí)區(qū)的日期和時(shí)間,是前兩者的結(jié)合
  • Duration :表示兩個(gè)時(shí)刻之間的時(shí)間間隔
  • Period :表示兩個(gè)日期之間的天數(shù)

三.實(shí)戰(zhàn)

3.1 LocalDate的創(chuàng)建與使用

3.1.1、LocalDate的創(chuàng)建

    /**
     * LocalDate的初始化方法
     * 此處單元測試的注解是采用:org.junit.Test
     */
    @Test
    public void init(){
        //1.創(chuàng)建LocalDate,LocalDate對(duì)象直接調(diào)用now(),獲取到當(dāng)前日期
        LocalDate localDateNow = LocalDate.now();
        System.out.println("1.直接調(diào)用now()創(chuàng)建:" + localDateNow);
        //2.直接根據(jù)(年月日)創(chuàng)建,如:2021-05-20
        LocalDate localDateOf = LocalDate.of(2021, 5, 20);
        System.out.println("2.根據(jù)年月日創(chuàng)建:" + localDateOf);
        //3.直接根據(jù)某一年的某一天創(chuàng)建,如 2021年中第200天
        LocalDate localDateOfYearDay = LocalDate.ofYearDay(2021, 200);
        System.out.println("3.根據(jù)某一年的某一天創(chuàng)建:" + localDateOfYearDay);
        //4.根據(jù)java.time.temporal.TemporalAccessor創(chuàng)建(包括LocalDate,LocalDateTime等等)
        LocalDate localDateFrom = LocalDate.from(LocalDate.now());
        System.out.println("4.根據(jù)TemporalAccessor創(chuàng)建:" + localDateFrom);
        //5.根據(jù)時(shí)間字符串轉(zhuǎn)化創(chuàng)建,調(diào)用parse(CharSequence text)方法,此方法只支持yyyy-MM-dd格式的值
        LocalDate localDateParse = LocalDate.parse("2021-05-11");
        System.out.println("5.根據(jù)時(shí)間字符串轉(zhuǎn)化創(chuàng)建:" + localDateParse);
        //6.根據(jù)時(shí)間字符串轉(zhuǎn)化創(chuàng)建,調(diào)用parse(CharSequence text, DateTimeFormatter formatter)方法,此時(shí)的text可以根據(jù)formatter多變
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate localDateParse2 = LocalDate.parse("20210515", formatter);
        System.out.println("6.根據(jù)時(shí)間字符串轉(zhuǎn)化創(chuàng)建:" + localDateParse2);
    }

運(yùn)行結(jié)果:

1.直接調(diào)用now()創(chuàng)建:2021-05-24
2.根據(jù)年月日創(chuàng)建:2021-05-20
3.根據(jù)某一年的某一天創(chuàng)建:2021-07-19
4.根據(jù)TemporalAccessor創(chuàng)建:2021-05-24
5.根據(jù)時(shí)間字符串轉(zhuǎn)化創(chuàng)建:2021-05-11
6.根據(jù)時(shí)間字符串轉(zhuǎn)化創(chuàng)建:2021-05-15

3.1.2、LocalDate的常見使用方法

   /**
     * LocalDate的常用使用方法
     * 此處單元測試的注解是采用:org.junit.Test
     */
    @Test
    public void usage() {
        //此處采用LocalDate對(duì)象直接調(diào)用now(),獲取到當(dāng)前日期,注意:如果使用我的實(shí)例,結(jié)果會(huì)不一樣,因?yàn)長ocalDate.now()是調(diào)用時(shí)的日期
        LocalDate currentDate = LocalDate.now();
        //1.獲取年、月、日、星期幾、月中天、年中天、月數(shù)
        System.out.println("------------------1.獲取年、月、日、星期幾、月中天、年中天、月數(shù)-----------------------");
        System.out.println("1.獲取到的當(dāng)前日期:" + 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.時(shí)間的計(jì)算
        System.out.println("-----------------2.時(shí)間的計(jì)算,主要是加減法------------------------");
        System.out.println("2.獲取到的當(dāng)前日期:" + currentDate);
        System.out.println("2.加法,當(dāng)前日期上加2年:" + currentDate.plusYears(2));
        System.out.println("2.加法,當(dāng)前日期上加3個(gè)月:" + currentDate.plusMonths(3));
        System.out.println("2.加法,當(dāng)前日期上加20天:" + currentDate.plusDays(20));
        System.out.println("2.加法,當(dāng)前日期上加一周:" + currentDate.plusWeeks(1));
        System.out.println("2.減法,當(dāng)前日期上減2年:" + currentDate.minusYears(2));
        System.out.println("2.減法,當(dāng)前日期上減3個(gè)月:" + currentDate.minusMonths(3));
        System.out.println("2.減法,當(dāng)前日期上減20天:" + currentDate.minusDays(20));
        System.out.println("2.減法,當(dāng)前日期上減一周:" + currentDate.minusWeeks(1));
        //3.時(shí)間的判斷及轉(zhuǎn)化
        System.out.println("-----------------3.時(shí)間的判斷及格式化(格式化的類型很多)------------------------");
        //新建一個(gè)格式化類型
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        //新創(chuàng)建一個(gè)LocalDate日期進(jìn)行比較
        LocalDate localDateOf = LocalDate.of(2020, 5, 20);
        System.out.println("3.當(dāng)前日期轉(zhuǎn)成yyyyMMdd型的字符串:" + currentDate.format(formatter));
        System.out.println("3.當(dāng)前日期是否在一個(gè)日期之后:" + currentDate.isAfter(localDateOf));
        System.out.println("3.當(dāng)前日期是否在一個(gè)日期之前:" + currentDate.isBefore(localDateOf));
        System.out.println("3.當(dāng)前日期是否是閏年:" + currentDate.isLeapYear());
        System.out.println("3.2020-05-20是否是閏年:" + localDateOf.isLeapYear());
        //4.根據(jù)指定數(shù)據(jù)獲取日期或者時(shí)間(LocalTime)
        System.out.println("-----------------4.根據(jù)指定數(shù)據(jù)獲取日期或者時(shí)間(LocalTime)------------------------");
        System.out.println("4.獲取到的當(dāng)前日期:" + currentDate);
        System.out.println("4.修改年數(shù)為1999年:" + currentDate.withYear(1999));
        System.out.println("4.修改月數(shù)為10月:" + currentDate.withMonth(10));
        System.out.println("4.修改天數(shù)為當(dāng)月12日:" + currentDate.withDayOfMonth(12));
        System.out.println("4.獲取到的當(dāng)前日期的開始時(shí)間:" + currentDate.atStartOfDay());
        System.out.println("4.根據(jù)指定的時(shí)、分、秒獲取時(shí)間:" + currentDate.atTime(12, 23, 45));
        System.out.println("4.根據(jù)時(shí)間LocalTime對(duì)象獲取時(shí)間:" + currentDate.atTime(LocalTime.now()));
    }

運(yùn)行結(jié)果:

------------------1.獲取年、月、日、星期幾、月中天、年中天、月數(shù)-----------------------
1.獲取到的當(dāng)前日期:2021-05-24
1.獲取到的年:2021
1.獲取到的月:5
1.獲取到的一月中的哪一天:24
1.獲取到的一周中的星期幾:1
1.獲取到的一年的第多少天:144
1.獲取到的一年有多少天:365
1.獲取到的一年有多少月:31
-----------------2.時(shí)間的計(jì)算,主要是加減法------------------------
2.獲取到的當(dāng)前日期:2021-05-24
2.加法,當(dāng)前日期上加2年:2023-05-24
2.加法,當(dāng)前日期上加3個(gè)月:2021-08-24
2.加法,當(dāng)前日期上加20天:2021-06-13
2.加法,當(dāng)前日期上加一周:2021-05-31
2.減法,當(dāng)前日期上減2年:2019-05-24
2.減法,當(dāng)前日期上減3個(gè)月:2021-02-24
2.減法,當(dāng)前日期上減20天:2021-05-04
2.減法,當(dāng)前日期上減一周:2021-05-17
-----------------3.時(shí)間的判斷及格式化(格式化的類型很多)------------------------
3.當(dāng)前日期轉(zhuǎn)成yyyyMMdd型的字符串:20210524
3.當(dāng)前日期是否在一個(gè)日期之后:true
3.當(dāng)前日期是否在一個(gè)日期之前:false
3.當(dāng)前日期是否是閏年:false
3.2020-05-20是否是閏年:true
-----------------4.根據(jù)指定數(shù)據(jù)獲取日期或者時(shí)間(LocalTime)------------------------
4.獲取到的當(dāng)前日期:2021-05-24
4.修改年數(shù)為1999年:1999-05-24
4.修改月數(shù)為10月:2021-10-24
4.修改天數(shù)為當(dāng)月12日:2021-05-12
4.獲取到的當(dāng)前日期的開始時(shí)間:2021-05-24T00:00
4.根據(jù)指定的時(shí)、分、秒獲取時(shí)間:2021-05-24T12:23:45
4.根據(jù)時(shí)間LocalTime對(duì)象獲取時(shí)間:2021-05-24T19:49:04.468

3.2 LocalTime的創(chuàng)建與使用

3.2.1、LocalTime創(chuàng)建

    /**
     * LocalTime的初始化方法
     * 此處單元測試的注解是采用:org.junit.Test
     */
    @Test
    public void init() {
        //1.LocalTime,LocalTime對(duì)象直接調(diào)用now(),獲取到當(dāng)前時(shí)間
        LocalTime now = LocalTime.now();
        System.out.println("1.獲取到的當(dāng)前時(shí)間:" + now);
        //2.根據(jù)指定的時(shí)分秒生成時(shí)間
        LocalTime localTimeOf = LocalTime.of(23, 59, 59);
        System.out.println("2.根據(jù)指定的時(shí)分秒生成時(shí)間:" + localTimeOf);
        //3.根據(jù)指定的時(shí)分秒生成時(shí)間
        LocalTime localTimeParse = LocalTime.parse("12:23:45");
        System.out.println("3.根據(jù)指定的時(shí)分秒生成時(shí)間:" + localTimeParse);
        //4.根據(jù)指定的時(shí)分秒和格式化類型生成時(shí)間
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmmss");//注意:HH是24小時(shí)制,hh是12小時(shí)制,時(shí)間類型的不要出現(xiàn)年月日如:yyyyMMdd
        LocalTime localTimeParseWithFormatter = LocalTime.parse("102008", formatter);
        System.out.println("4.根據(jù)指定的時(shí)分秒和格式化類型生成時(shí)間:" + localTimeParseWithFormatter);
        //5.根據(jù)時(shí)間對(duì)象TemporalAccessor生成
        LocalTime initTime = LocalTime.of(11, 59, 59);
        System.out.println("5.根據(jù)時(shí)間對(duì)象TemporalAccessor生成:" + LocalTime.from(initTime));
    }

運(yùn)行結(jié)果:

1.獲取到的當(dāng)前時(shí)間:11:36:05.740
2.根據(jù)指定的時(shí)分秒生成時(shí)間:23:59:59
3.根據(jù)指定的時(shí)分秒生成時(shí)間:12:23:45
4.根據(jù)指定的時(shí)分秒和格式化類型生成時(shí)間:10:20:08
5.根據(jù)時(shí)間對(duì)象TemporalAccessor生成:11:59:59

3.2.2、LocalTime的常見使用方法

/**
     * LocalTime的常用使用方法
     * 此處單元測試的注解是采用:org.junit.Test
     * 和LocalDate的操作方法差不多
     */
    @Test
    public void usage() {
        //此處采用LocalTime對(duì)象直接調(diào)用now(),獲取到當(dāng)前時(shí)間,注意:如果使用我的實(shí)例,結(jié)果會(huì)不一樣,因?yàn)長ocalTime.now()是調(diào)用時(shí)的時(shí)間
        LocalTime currentTime = LocalTime.now();
        //1.獲取時(shí)、分、秒
        System.out.println("------------------1.獲取時(shí)、分、秒-----------------------");
        System.out.println("1.獲取到的當(dāng)前時(shí)間:" + currentTime);
        System.out.println("1.獲取當(dāng)前時(shí)間的小時(shí):" + currentTime.getHour());
        System.out.println("1.獲取當(dāng)前時(shí)間的分鐘:" + currentTime.getMinute());
        System.out.println("1.獲取當(dāng)前時(shí)間的秒:" + currentTime.getSecond());
        System.out.println("1.獲取當(dāng)前時(shí)間的秒:" + currentTime.getNano());
        //2.時(shí)間的加減
        System.out.println("------------------2.時(shí)間的加減-----------------------");
        System.out.println("2.獲取到的當(dāng)前時(shí)間:" + currentTime);
        System.out.println("2.加法:當(dāng)前時(shí)間上增加1小時(shí)" + currentTime.plusHours(1));
        System.out.println("2.加法:當(dāng)前時(shí)間上增加10分鐘" + currentTime.plusMinutes(10));
        System.out.println("2.加法:當(dāng)前時(shí)間上增加20秒" + currentTime.plusSeconds(20));
        System.out.println("2.減法:當(dāng)前時(shí)間上減加2小時(shí)" + currentTime.minusHours(2));
        System.out.println("2.減法:當(dāng)前時(shí)間上減加30分鐘" + currentTime.minusMinutes(30));
        System.out.println("2.減法:當(dāng)前時(shí)間上減加5秒" + currentTime.minusSeconds(5));
        System.out.println("------------------3.時(shí)間的判斷及轉(zhuǎn)化-----------------------");
        //初始化一個(gè)新的時(shí)間
        LocalTime initTime = LocalTime.of(13, 59, 59);
        System.out.println("3.獲取到的當(dāng)前時(shí)間:" + currentTime);
        System.out.println("3.新初始化的時(shí)間:" + initTime);
        System.out.println("3.判斷當(dāng)前時(shí)間是否是一個(gè)時(shí)間之后:" + currentTime.isAfter(initTime));
        System.out.println("3.判斷當(dāng)前時(shí)間是否是一個(gè)時(shí)間之前:" + currentTime.isBefore(initTime));
        System.out.println("3.修改小時(shí)數(shù)為12:" + currentTime.withHour(12));
        System.out.println("3.修改分鐘數(shù)為12:" + currentTime.withMinute(12));
        System.out.println("3.修改秒數(shù)為12:" + currentTime.withSecond(12));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime parseTime = LocalTime.parse("11:45:28", formatter);
        System.out.println("3.根據(jù)指定的時(shí)分秒和格式化類型生成時(shí)間:" + parseTime);
    }

運(yùn)行結(jié)果:

------------------1.獲取時(shí)、分、秒-----------------------
1.獲取到的當(dāng)前時(shí)間:19:50:14.612
1.獲取當(dāng)前時(shí)間的小時(shí):19
1.獲取當(dāng)前時(shí)間的分鐘:50
1.獲取當(dāng)前時(shí)間的秒:14
1.獲取當(dāng)前時(shí)間的秒:612000000
------------------2.時(shí)間的加減-----------------------
2.獲取到的當(dāng)前時(shí)間:19:50:14.612
2.加法:當(dāng)前時(shí)間上增加1小時(shí)20:50:14.612
2.加法:當(dāng)前時(shí)間上增加10分鐘20:00:14.612
2.加法:當(dāng)前時(shí)間上增加20秒19:50:34.612
2.減法:當(dāng)前時(shí)間上減加2小時(shí)17:50:14.612
2.減法:當(dāng)前時(shí)間上減加30分鐘19:20:14.612
2.減法:當(dāng)前時(shí)間上減加5秒19:50:09.612
------------------3.時(shí)間的判斷及轉(zhuǎn)化-----------------------
3.獲取到的當(dāng)前時(shí)間:19:50:14.612
3.新初始化的時(shí)間:13:59:59
3.判斷當(dāng)前時(shí)間是否是一個(gè)時(shí)間之后:true
3.判斷當(dāng)前時(shí)間是否是一個(gè)時(shí)間之前:false
3.修改小時(shí)數(shù)為12:12:50:14.612
3.修改分鐘數(shù)為12:19:12:14.612
3.修改秒數(shù)為12:19:50:12.612
3.根據(jù)指定的時(shí)分秒和格式化類型生成時(shí)間:11:45:28

3.3 LocalDateTime的創(chuàng)建與使用

3.3.1、LocalDateTime創(chuàng)建

/**
     * LocalDateTime的初始化方法
     * 此處單元測試的注解是采用:org.junit.Test
     */
    @Test
    public void init() {
        //1.LocalDateTime對(duì)象直接調(diào)用now(),獲取到當(dāng)前時(shí)間
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("1.LocalDateTime對(duì)象直接調(diào)用now()獲取到的時(shí)間:" + localDateTime);
        //2.根據(jù)年月日時(shí)分秒構(gòu)造(此處方法比較多,不一一介紹)
        LocalDateTime localDateTimeOf = LocalDateTime.of(2021, 5, 10, 18, 30, 26);
        System.out.println("2.根據(jù)年月日時(shí)分秒構(gòu)造獲取到的時(shí)間:" + localDateTimeOf);
        //3.根據(jù)LocalDate和LocalTime得到(在有日期和時(shí)間的情況下可以使用)
        LocalDateTime of = LocalDateTime.of(LocalDate.now(), LocalTime.now());
        System.out.println("3.根據(jù)LocalDate和LocalTime得到:" + of);
        //4.LocalDate指定一個(gè)LocalTime(LocalDate只有年月日)
        LocalTime localTimeInit = LocalTime.of(14, 25, 25);
        LocalDateTime localDateWithLocalTime = LocalDate.now().atTime(localTimeInit);
        System.out.println("4.LocalDate指定一個(gè)LocalTime:" + localDateWithLocalTime);
        //5.LocalTime指定一個(gè)LocalDate(LocalTime只有時(shí)分秒)
        LocalDate localDateInit = LocalDate.of(1998, 10, 1);
        LocalDateTime localTimeWithLocalDate = LocalTime.now().atDate(localDateInit);
        System.out.println("5.LocalTime指定一個(gè)LocalDate:" + localTimeWithLocalDate);
    }

運(yùn)行結(jié)果:

1.LocalDateTime對(duì)象直接調(diào)用now()獲取到的時(shí)間:2021-05-24T19:51:15.237
2.根據(jù)年月日時(shí)分秒構(gòu)造獲取到的時(shí)間:2021-05-10T18:30:26
3.根據(jù)LocalDate和LocalTime得到:2021-05-24T19:51:15.238
4.LocalDate指定一個(gè)LocalTime:2021-05-24T14:25:25
5.LocalTime指定一個(gè)LocalDate:1998-10-01T19:51:15.238

3.3.2、LocalDateTime的常見使用方法

    /**
     * LocalDateTime的常用使用方法
     * 此處單元測試的注解是采用:org.junit.Test
     */
    @Test
    public void usage() {
        //1.根據(jù)LocalDateTime獲取LocalDate
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println("1.根據(jù)LocalDateTime獲取LocalDate: " + currentTime.toLocalDate());
        //2.根據(jù)LocalDateTime獲取LocalTime
        System.out.println("2.根據(jù)LocalDateTime獲取LocalTime: " + currentTime.toLocalTime());
        System.out.println("------------------3.時(shí)間的加減法及修改-----------------------");
        //3.LocalDateTime的加減法包含了LocalDate和LocalTime的所有加減,上面說過,這里就只做簡單介紹
        System.out.println("3.當(dāng)前時(shí)間:" + currentTime);
        System.out.println("3.當(dāng)前時(shí)間加5年:" + currentTime.plusYears(5));
        System.out.println("3.當(dāng)前時(shí)間加2個(gè)月:" + currentTime.plusMonths(2));
        System.out.println("3.當(dāng)前時(shí)間減2天:" + currentTime.minusDays(2));
        System.out.println("3.當(dāng)前時(shí)間減5個(gè)小時(shí):" + currentTime.minusHours(5));
        System.out.println("3.當(dāng)前時(shí)間加5分鐘:" + currentTime.plusMinutes(5));
        System.out.println("3.當(dāng)前時(shí)間加20秒:" + currentTime.plusSeconds(20));
        //還可以靈活運(yùn)用比如:向后加一年,向前減一天,向后加2個(gè)小時(shí),向前減5分鐘,可以進(jìn)行連寫
        System.out.println("3.同時(shí)修改(向后加一年,向前減一天,向后加2個(gè)小時(shí),向前減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.修改小時(shí)為12:" + currentTime.withHour(12));
        System.out.println("3.修改分鐘為12:" + currentTime.withMinute(12));
        System.out.println("3.修改秒為12:" + currentTime.withSecond(12));
        System.out.println("------------------4.時(shí)間的轉(zhuǎn)化及其他-----------------------");
        //4.時(shí)間的格式化及其他
        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.時(shí)間字符串轉(zhuǎn)為時(shí)間:" + 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.所屬月份的下個(gè)月的第一天:" + localDate.with(firstDayOfNextMonth()));
    }

運(yùn)行結(jié)果:

1.根據(jù)LocalDateTime獲取LocalDate: 2021-05-24
2.根據(jù)LocalDateTime獲取LocalTime: 19:57:46.316
------------------3.時(shí)間的加減法及修改-----------------------
3.當(dāng)前時(shí)間:2021-05-24T19:57:46.316
3.當(dāng)前時(shí)間加5年:2026-05-24T19:57:46.316
3.當(dāng)前時(shí)間加2個(gè)月:2021-07-24T19:57:46.316
3.當(dāng)前時(shí)間減2天:2021-05-22T19:57:46.316
3.當(dāng)前時(shí)間減5個(gè)小時(shí):2021-05-24T14:57:46.316
3.當(dāng)前時(shí)間加5分鐘:2021-05-24T20:02:46.316
3.當(dāng)前時(shí)間加20秒:2021-05-24T19:58:06.316
3.同時(shí)修改(向后加一年,向前減一天,向后加2個(gè)小時(shí),向前減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.修改小時(shí)為12:2021-05-24T12:57:46.316
3.修改分鐘為12:2021-05-24T19:12:46.316
3.修改秒為12:2021-05-24T19:57:12.316
------------------4.時(shí)間的轉(zhuǎn)化及其他-----------------------
4.時(shí)間字符串轉(zhuǎn)為時(shí)間: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.所屬月份的下個(gè)月的第一天:2021-06-01

到此這篇關(guān)于Java中LocalDateTime的具體用法的文章就介紹到這了,更多相關(guān)Java LocalDateTime內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis中foreach標(biāo)簽的collection屬性的取值方式

    MyBatis中foreach標(biāo)簽的collection屬性的取值方式

    這篇文章主要介紹了MyBatis中foreach標(biāo)簽的collection屬性的取值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java 高并發(fā)編程之最實(shí)用的任務(wù)執(zhí)行架構(gòu)設(shè)計(jì)建議收藏

    Java 高并發(fā)編程之最實(shí)用的任務(wù)執(zhí)行架構(gòu)設(shè)計(jì)建議收藏

    高并發(fā)(High Concurrency)是互聯(lián)網(wǎng)分布式系統(tǒng)架構(gòu)設(shè)計(jì)中必須考慮的因素之一,它通常是指,通過設(shè)計(jì)保證系統(tǒng)能夠同時(shí)并行處理很多請(qǐng)求,高并發(fā)相關(guān)常用的一些指標(biāo)有響應(yīng)時(shí)間(Response Time),吞吐量(Throughput),每秒查詢率QPS(Query Per Second),并發(fā)用戶數(shù)等
    2021-10-10
  • springboot項(xiàng)目組引入JMeter的實(shí)現(xiàn)步驟

    springboot項(xiàng)目組引入JMeter的實(shí)現(xiàn)步驟

    本文主要介紹了springboot項(xiàng)目組引入JMeter的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Spring Boot讀取自定義配置文件

    Spring Boot讀取自定義配置文件

    在Spring Boot項(xiàng)目中我們經(jīng)常需要讀取application.yml配置文件的自定義配置,今天就來羅列一下從yaml讀取配置文件的一些常用手段和方法。
    2021-05-05
  • SpringBoot整合MyBatis超詳細(xì)教程

    SpringBoot整合MyBatis超詳細(xì)教程

    這篇文章主要介紹了SpringBoot整合MyBatis超詳細(xì)教程,下面從配置模式、注解模式、混合模式三個(gè)方面進(jìn)行說明MyBatis與SpringBoot的整合,需要的朋友可以參考下
    2021-05-05
  • 關(guān)于springcloud報(bào)錯(cuò)報(bào)UnsatisfiedDependencyException的問題

    關(guān)于springcloud報(bào)錯(cuò)報(bào)UnsatisfiedDependencyException的問題

    這篇文章主要介紹了關(guān)于springcloud報(bào)錯(cuò)報(bào)UnsatisfiedDependencyException的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Flink開發(fā)IDEA環(huán)境搭建與測試的方法

    Flink開發(fā)IDEA環(huán)境搭建與測試的方法

    這篇文章主要介紹了Flink開發(fā)IDEA環(huán)境搭建與測試的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • @FeignClient之name,value,url詳解

    @FeignClient之name,value,url詳解

    在FeignClient中,`name`用于指定服務(wù)的名稱,通常與服務(wù)注冊(cè)中心中的服務(wù)名關(guān)聯(lián),而`url`用于指定請(qǐng)求的基礎(chǔ)URL,適用于不使用服務(wù)注冊(cè)的場景,如果同時(shí)配置了`name`和`url`,則`url`會(huì)優(yōu)先生效,Feign會(huì)直接使用`url`指定的地址
    2024-11-11
  • java實(shí)現(xiàn)mysql操作類分享 java連接mysql

    java實(shí)現(xiàn)mysql操作類分享 java連接mysql

    這篇文章主要介紹了java實(shí)現(xiàn)的mysql操作類示例,大家在連接數(shù)據(jù)的時(shí)候可以直接使用了
    2014-01-01
  • springboot整合websocket實(shí)現(xiàn)群聊思路代碼詳解

    springboot整合websocket實(shí)現(xiàn)群聊思路代碼詳解

    通過springboot引入websocket,實(shí)現(xiàn)群聊,通過在線websocket測試進(jìn)行展示。本文重點(diǎn)給大家介紹springboot整合websocket實(shí)現(xiàn)群聊功能,代碼超級(jí)簡單,感興趣的朋友跟隨小編一起學(xué)習(xí)吧
    2021-05-05

最新評(píng)論

类乌齐县| 贵定县| 从化市| 连城县| 夏邑县| 友谊县| 武胜县| 盐亭县| 会东县| 靖安县| 蓝田县| 扶绥县| 江达县| 康保县| 临猗县| 通榆县| 灵武市| 微博| 平南县| 平阴县| 靖安县| 斗六市| 桃园县| 洱源县| 栖霞市| 临猗县| 扎鲁特旗| 康平县| 淳化县| 宝兴县| 探索| 桐柏县| 平远县| 黎城县| 鄯善县| 沐川县| 卢龙县| 镇坪县| 城步| 聂拉木县| 称多县|