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

Java?LocalDateTime常用操作方法

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

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

LocalDateTime localDateTime = LocalDateTime.now();
        //時(shí)間運(yùn)算,相加相減
        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()); //獲取時(shí)間(LocalTime) 結(jié)果:15:36:12.914
        System.out.println(localDateTime.getDayOfMonth()); //獲取當(dāng)前時(shí)間月份的第幾天 結(jié)果:24
        System.out.println(localDateTime.getDayOfWeek());  //獲取當(dāng)前周的第幾天       結(jié)果:WEDNESDAY
        System.out.println(localDateTime.getDayOfYear());  //獲取當(dāng)前時(shí)間在該年屬于第幾天 結(jié)果:328
        System.out.println(localDateTime.getMonthValue()); // 獲取當(dāng)前時(shí)間的月份(阿拉伯文) 結(jié)果:11
        System.out.println(localDateTime.getMonth());      // 獲取當(dāng)前時(shí)間的月份(英文) 結(jié)果:11
        System.out.println(localDateTime.getHour());       // 獲取當(dāng)前時(shí)間的小時(shí)數(shù) 結(jié)果:15
        System.out.println(localDateTime.getMinute());     // 獲取當(dāng)前時(shí)間的分鐘數(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)造時(shí)間
        LocalDateTime startTime = LocalDateTime.of(2021, 1, 1, 20, 31, 20);
        LocalDateTime endTime = LocalDateTime.of(2021, 1, 3, 20, 31, 20);
        //比較時(shí)間
        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
        // 獲取當(dāng)前時(shí)間的0點(diǎn)~23點(diǎn)
        LocalDateTime beginDay = localDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDay = localDateTime.withHour(23).withMinute(59).withSecond(59);
        // 獲取本月的第一天的0點(diǎn)0分0秒和最后一天的23點(diǎn)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實(shí)用方法的文章就介紹到這了,更多相關(guān)Java LocalDateTime實(shí)用方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

贵阳市| 增城市| 池州市| 遂川县| 阿鲁科尔沁旗| 崇文区| 都江堰市| 岳池县| 丁青县| 阿拉善左旗| 灌南县| 栾城县| 淳安县| 南乐县| 灌南县| 资阳市| 临江市| 华容县| 阿巴嘎旗| 彭泽县| 博爱县| 高阳县| 桂平市| 哈尔滨市| 施甸县| 沈阳市| 新乡市| 湘乡市| 普定县| 普兰店市| 黄平县| 自贡市| 武威市| 法库县| 白朗县| 哈密市| 大同县| 舒兰市| 木兰县| 兰坪| 禄劝|