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

Java中常用時(shí)間的一些相關(guān)方法

 更新時(shí)間:2021年10月26日 11:52:36   作者:影子930  
日期的使用多種多樣,但萬(wàn)變不離其宗,下面這篇文章主要給大家介紹了關(guān)于Java中常用時(shí)間的一些相關(guān)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

在我們java開(kāi)發(fā)中,Date日期這個(gè)字段會(huì)被經(jīng)常使用,比如獲取當(dāng)前系統(tǒng)的時(shí)間,獲取上個(gè)月,上一年的時(shí)間,以及獲取兩個(gè)日期相差的時(shí)分秒數(shù),或者對(duì)日期類型進(jìn)行格式化,等等,等等,下面將給大家詳細(xì)介紹下Java中常用時(shí)間的一些相關(guān)方法

一、獲取當(dāng)前時(shí)間的方式

public static void main(String[] args) {
    //Date
    Date now = new Date();
    System.out.println(now);

    //java8的時(shí)間
    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println(localDateTime);


    Calendar calendar = Calendar.getInstance();
    Date time = calendar.getTime();
    System.out.println(time);
    System.out.println("年" + calendar.get(Calendar.YEAR));
    System.out.println("月" + (calendar.get(Calendar.MONTH) + 1));

    //joda time
    DateTime dateTime = DateTime.now();
    System.out.println(dateTime);
}


獲取當(dāng)前時(shí)間可以使用Date LocalDatetime Calendar  Datetime

二、獲取當(dāng)月第n天

public static void main(String[] args) {
    //建議使用Calendar  可以設(shè)置年月日時(shí)分秒
    Calendar calendar = Calendar.getInstance();
    ////當(dāng)月16
    calendar.set(Calendar.DAY_OF_MONTH, 16);
    System.out.println(calendar.getTime());

    //當(dāng)月16
    DateTime now = DateTime.now();
    DateTime dateTime = now.withDayOfMonth(16);
    System.out.println(dateTime);

    //當(dāng)月14
    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println(localDateTime.withDayOfMonth(14));

    //1月11
    System.out.println(localDateTime.withMonth(1).withDayOfMonth(11));
}

三、格式化為字符串

```
//使用SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(new Date()));

//使用Calendar
Calendar calendar = Calendar.getInstance();
System.out.println(String.format("%s年%s月%s日%s時(shí)%s分%s秒", calendar.get(Calendar.YEAR),
        calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH),
        calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)));

LocalDateTime now = LocalDateTime.now();
String str = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(str);
```

四、加減時(shí)間(單位可以是秒,小時(shí)等)

public static void main(String[] args) {
    Date now = new Date();
    //加一小時(shí)
    long time = now.getTime() + (60 * 60 * 1000);
    System.out.println(new Date(time));

    /*
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.7.14</version>
    </dependency>
     */
    //引入Hutool 加一小時(shí)
    System.out.println(DateUtil.offset(now, DateField.HOUR, 1));
    //減一小時(shí)
    System.out.println(DateUtil.offset(now, DateField.HOUR, -1));

    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println("加一小時(shí)" + localDateTime.plusHours(1));
    System.out.println("減一小時(shí)" + localDateTime.minusHours(1));

    DateTime dateTime = DateTime.now();
    System.out.println(dateTime.plusHours(1));
    System.out.println(dateTime.minusHours(1));
}

LocalDateTime和DateTime都自帶增加和減少時(shí)間的方法

五、通過(guò)出生日期獲取年齡

public static void main(String[] args) {
    //時(shí)間1990-12-05
    DateTime birthDay = DateTime.now().withYear(1990).withMonthOfYear(10).withDayOfMonth(23);
    System.out.println(birthDay);
    //獲取相差得年 會(huì)進(jìn)行月份和日期比較 如
    Years years = Years.yearsBetween(birthDay, new DateTime());
    System.out.println(years);
    System.out.println(years.getYears());
}

還可以使用年份相減,再比較月,日的方法得到生日

六、判斷兩個(gè)時(shí)間段是否覆蓋

public static void main(String[] args) {
    DateTime now = DateTime.now();

    DateTime start1 = now;
    DateTime end1 = now.plusMinutes(1);

    DateTime start2 = now.plusSeconds(50);
    DateTime end2 = now.plusMinutes(2);

    Interval interval1 = new Interval(start1, end1);
    Interval interval2 = new Interval(start2, end2);

    System.out.println(interval1.overlaps(interval2));
    System.out.println(start1.getMillis() < end2.getMillis() && start2.getMillis() < end1.getMillis());
}

七、求兩個(gè)時(shí)間間隔

public static void main(String[] args) {
    DateTime now = DateTime.now();
    //開(kāi)始時(shí)間
    Date startTime = now.toDate();
    //結(jié)束時(shí)間
    Date endTime = now.plusHours(1).toDate();
    //1小時(shí)
    System.out.println("開(kāi)始時(shí)間與結(jié)束時(shí)間的時(shí)間間隔:" + DateUtil.between(startTime, endTime, DateUnit.SECOND));

    long time = (endTime.getTime() - startTime.getTime()) / 1000;
    System.out.println(time);
}

八、UTC時(shí)間與北京時(shí)間轉(zhuǎn)換

public static void main(String[] args) throws ParseException {
    Date now = new Date();
    Date utcDate = bj2UTC(now);
    //utc時(shí)間 
    System.out.println(utcDate);
    //北京時(shí)間
    System.out.println(utc2BJ(utcDate));

    DateTime dateTime = DateTime.now().withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);
    System.out.println(dateTime);
    System.out.println(bj2UTC(dateTime.toDate()));
}

public static Date bj2UTC(Date date) {
    if (date == null) {
        return null;
    }
    LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("-8"));
    return Date.from(localDateTime.atOffset(ZoneOffset.UTC).toInstant());
}

public static Date utc2BJ(Date date) {
    if (date == null) {
        return null;
    }
    LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("+8"));
    return Date.from(localDateTime.atOffset(ZoneOffset.UTC).toInstant());
}

北京時(shí)間=UTC+8

總結(jié)

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

相關(guān)文章

  • SpringBoot之跨域過(guò)濾器配置允許跨域訪問(wèn)方式

    SpringBoot之跨域過(guò)濾器配置允許跨域訪問(wèn)方式

    這篇文章主要介紹了SpringBoot之跨域過(guò)濾器配置允許跨域訪問(wèn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 為Java應(yīng)用創(chuàng)建Docker鏡像的3種方式總結(jié)

    為Java應(yīng)用創(chuàng)建Docker鏡像的3種方式總結(jié)

    Docker的使用可以將應(yīng)用程序做成鏡像,這樣可以將鏡像發(fā)布到私有或者公有倉(cāng)庫(kù)中,在其他主機(jī)上也可以pull鏡像,并且運(yùn)行容器,運(yùn)行程,下面這篇文章主要給大家總結(jié)介紹了關(guān)于為Java應(yīng)用創(chuàng)建Docker鏡像的3種方式,需要的朋友可以參考下
    2023-06-06
  • SpringBoot文件分片上傳教程

    SpringBoot文件分片上傳教程

    這篇文章主要介紹了SpringBoot文件分片上傳教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 解決myBatis中openSession()自動(dòng)提交的問(wèn)題

    解決myBatis中openSession()自動(dòng)提交的問(wèn)題

    在學(xué)習(xí)MySQL過(guò)程中,發(fā)現(xiàn)插入操作自動(dòng)提交,問(wèn)題原因可能是myBatis中的openSession()方法設(shè)置了自動(dòng)提交,或者是MySQL的默認(rèn)引擎設(shè)置為不支持事務(wù)的MyISAM,解決辦法包括更改myBatis的提交設(shè)置或?qū)ySQL表的引擎改為InnoDB
    2024-09-09
  • springboot登陸過(guò)濾功能的實(shí)現(xiàn)代碼

    springboot登陸過(guò)濾功能的實(shí)現(xiàn)代碼

    這篇文章主要介紹了springboot登陸過(guò)濾功能的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Java中String類使用方法總結(jié)

    Java中String類使用方法總結(jié)

    這篇文章主要介紹了Java中String類的使用方法,文章簡(jiǎn)單易懂,結(jié)尾有實(shí)例代碼幫助大家理解學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • SpringCloud OpenFeign基本介紹與實(shí)現(xiàn)示例

    SpringCloud OpenFeign基本介紹與實(shí)現(xiàn)示例

    OpenFeign源于Netflix的Feign,是http通信的客戶端。屏蔽了網(wǎng)絡(luò)通信的細(xì)節(jié),直接面向接口的方式開(kāi)發(fā),讓開(kāi)發(fā)者感知不到網(wǎng)絡(luò)通信細(xì)節(jié)。所有遠(yuǎn)程調(diào)用,都像調(diào)用本地方法一樣完成
    2023-02-02
  • java數(shù)據(jù)結(jié)構(gòu)排序算法之樹形選擇排序詳解

    java數(shù)據(jù)結(jié)構(gòu)排序算法之樹形選擇排序詳解

    這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)排序算法之樹形選擇排序,結(jié)合具體實(shí)例形式分析了java樹形選擇排序的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-05-05
  • 從dubbo源碼分析qos-server端口沖突問(wèn)題及解決

    從dubbo源碼分析qos-server端口沖突問(wèn)題及解決

    這篇文章主要介紹了從dubbo源碼分析qos-server端口沖突問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • SpringBoot整合POI實(shí)現(xiàn)Excel文件讀寫操作

    SpringBoot整合POI實(shí)現(xiàn)Excel文件讀寫操作

    EasyExcel是一個(gè)基于Java的、快速、簡(jiǎn)潔、解決大文件內(nèi)存溢出的Excel處理工具,這篇文章主要介紹了SpringBoot整合POI實(shí)現(xiàn)Excel文件讀寫操作,首先準(zhǔn)備環(huán)境進(jìn)行一系列操作,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2023-10-10

最新評(píng)論

霍城县| 广丰县| 邵武市| 丰镇市| 庆云县| 旬阳县| 新源县| 白水县| 托克托县| 濉溪县| 遂昌县| 古浪县| 肇东市| 济宁市| 蚌埠市| 特克斯县| 古浪县| 濉溪县| 揭东县| 瑞安市| 元氏县| 咸宁市| 定兴县| 双流县| 隆德县| 霍山县| 福州市| 夹江县| 南部县| 正阳县| 丹阳市| 巢湖市| 微山县| 呼和浩特市| 广宁县| 习水县| 奉新县| 石景山区| 永胜县| 横山县| 张家界市|