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

Java?LocalDateTime常用操作方法

 更新時間:2022年01月17日 08:37:45   作者:請叫我頭頭哥  
這篇文章主要介紹了Java?LocalDateTime實用方法,Java8提供了新的時間接口LocalDateTime,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

Java8提供了新的時間接口LocalDateTime。本文主要介紹了Java8中LocalDateTime的一些常用操作方法。不多說,直接上代碼。欲知詳情,可以看看官網(wǎng)。

LocalDateTime localDateTime = LocalDateTime.now();
        //時間運算,相加相減
        System.out.println(localDateTime.plusYears(2)); //加2年
        System.out.println(localDateTime.plusDays(2)); //加兩天
        System.out.println(localDateTime.minusYears(2)); //減兩年
        System.out.println(localDateTime.minusDays(2)); //減兩天
        System.out.println(localDateTime.toString());    // 轉(zhuǎn)字符串 結(jié)果:2021-11-24T15:36:12.914
        System.out.println(localDateTime.toLocalDate()); //獲取日期(LocalDate) 結(jié)果:2021-11-24
        System.out.println(localDateTime.toLocalTime()); //獲取時間(LocalTime) 結(jié)果:15:36:12.914
        System.out.println(localDateTime.getDayOfMonth()); //獲取當前時間月份的第幾天 結(jié)果:24
        System.out.println(localDateTime.getDayOfWeek());  //獲取當前周的第幾天       結(jié)果:WEDNESDAY
        System.out.println(localDateTime.getDayOfYear());  //獲取當前時間在該年屬于第幾天 結(jié)果:328
        System.out.println(localDateTime.getMonthValue()); // 獲取當前時間的月份(阿拉伯文) 結(jié)果:11
        System.out.println(localDateTime.getMonth());      // 獲取當前時間的月份(英文) 結(jié)果:11
        System.out.println(localDateTime.getHour());       // 獲取當前時間的小時數(shù) 結(jié)果:15
        System.out.println(localDateTime.getMinute());     // 獲取當前時間的分鐘數(shù) 結(jié)果:36
        //格式化輸出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");
        System.out.println(localDateTime.format(formatter)); // 結(jié)果:2021/11/24 15:36:12
        //構(gòu)造時間
        LocalDateTime startTime = LocalDateTime.of(2021, 1, 1, 20, 31, 20);
        LocalDateTime endTime = LocalDateTime.of(2021, 1, 3, 20, 31, 20);
        //比較時間
        System.out.println(localDateTime.isAfter(startTime)); // 結(jié)果:true
        System.out.println(localDateTime.isBefore(endTime));  // 結(jié)果:false
        //獲取毫秒數(shù)(使用Instant)
        System.out.println(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); // 結(jié)果:1637739570129
        //獲取秒數(shù)(使用Instant)
        System.out.println(localDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond()); // 結(jié)果:1637739570
        // 獲取當前時間的0點~23點
        LocalDateTime beginDay = localDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDay = localDateTime.withHour(23).withMinute(59).withSecond(59);
        // 獲取本月的第一天的0點0分0秒和最后一天的23點59分59秒
        LocalDateTime beginMonth = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endMonth = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
        // LocalDateTime轉(zhuǎn)Date
        Date date = Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
        // Date轉(zhuǎn)LocalDateTime
        date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        // LocalDateTime獲取秒數(shù)
        Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        // LocalDateTime獲取毫秒數(shù)
        Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();

效果如下:

v源碼地址

https://github.com/toutouge/javademosecond/tree/master/hellolearn

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

相關(guān)文章

最新評論

斗六市| 和顺县| 南乐县| 枣阳市| 繁昌县| 西青区| 德江县| 竹北市| 汕头市| 酒泉市| 教育| 龙井市| 丰台区| 巴林右旗| 衡山县| 通许县| 红安县| 阳朔县| 邹平县| 柘城县| 项城市| 嵊州市| 海盐县| 同江市| 长武县| 永川市| 远安县| 柳林县| 白水县| 政和县| 科技| 南漳县| 石景山区| 泊头市| 石阡县| 太谷县| 儋州市| 建平县| 桓台县| 石泉县| 永川市|