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

詳解Java關(guān)于JDK中時(shí)間日期的API

 更新時(shí)間:2021年09月13日 16:21:25   作者:威斯布魯克.猩猩  
這篇文章主要介紹了詳解Java關(guān)于JDK中時(shí)間日期的API,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

JDK 8 之前日期和時(shí)間的API測(cè)試

//1.System類中的currentTimeMillis()
    public void test1(){
        long time = System.currentTimeMillis();
        //返回當(dāng)前時(shí)間與1970年1月1日0時(shí)0分0秒之間以毫秒為時(shí)間為單位的時(shí)間差。
        //稱為時(shí)間戳
        System.out.println(time);//1628416744054
    }

  /*
    java.util.Date類
            |---java.sql.Date類(兩個(gè)是子父類的關(guān)系)
    1.兩個(gè)構(gòu)造器的使用
        >構(gòu)造器一:Date():創(chuàng)建一個(gè)對(duì)應(yīng)當(dāng)前時(shí)間的Date對(duì)象
        >構(gòu)造器二:創(chuàng)建指定毫秒數(shù)的Date對(duì)象
    2.兩個(gè)方法的使用
        >toString():顯示當(dāng)前的年、月、日、時(shí)、分、秒
        >getTime():獲取當(dāng)前Date對(duì)象對(duì)應(yīng)的毫秒數(shù)。(時(shí)間戳)
    3.java.sql.Date對(duì)應(yīng)著數(shù)據(jù)庫(kù)中的日期類型的變量
        >如何實(shí)例化
        >如何將java.util.Date對(duì)象轉(zhuǎn)換為java.sql.Date對(duì)象
     */
    @Test
    public void test2(){
        //構(gòu)造器一:Date():創(chuàng)建一個(gè)對(duì)應(yīng)當(dāng)前時(shí)間的Date對(duì)象
        Date  date1 = new Date();
        System.out.println(date1.toString());//Sun Aug 08 18:07:27 CST 2021
        //構(gòu)造器二:創(chuàng)建指定毫秒數(shù)的Date對(duì)象
        Date  date2 = new Date(1534798293927L);
        System.out.println(date2.toString());//Tue Aug 21 04:51:33 CST 2018
        //創(chuàng)建java.sql.Date對(duì)象
        java.sql.Date date3 = new java.sql.Date(237493269533L);
        System.out.println(date3);//1977-07-12
        //如何將java.util.Date對(duì)象轉(zhuǎn)換為java.sql.Date對(duì)象
        //情況一:(面向?qū)ο?
//        Date date4 = new java.sql.Date(23682368236343L);
//        java.sql.Date date5 = (java.sql.Date)date4;
        //情況二:
        Date date6 = new Date();
        java.sql.Date date7 = new java.sql.Date(date6.getTime());
    }

 /*
    SimpleDateFormat的使用:SimpleDateFormat對(duì)日期Date類的格式化和解析
    1.兩個(gè)操作:
    1.1 格式化:日期--->字符串
    1.2 解析:格式化的逆過(guò)程:字符串--->日期
    2.SimpleDateFormat的實(shí)例化
     */
    @Test
    public void testSimpleDateFormat() throws ParseException {
        //實(shí)例化SimpleDateFormat
        SimpleDateFormat sdf = new SimpleDateFormat();
        //格式化:日期--->字符串
        Date date = new Date();
        System.out.println(date);
        String format = sdf.format(date);
        System.out.println(format);
        //解析:格式化的逆過(guò)程,字符串--->日期
        String str = "21-8-9 下午3:17";
        Date date1 = sdf.parse(str);
        System.out.println(date1);
        //******************按照指定的方式格式化和解析:調(diào)用帶參的構(gòu)造器*****************************
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm ss");
        //格式化
        String format1 = sdf1.format(date);
        System.out.println(format1);//2021-08-09 04:02 13
        //解析:要求字符串必須是符合SimpleDateFormat識(shí)別的格式(通過(guò)構(gòu)造器參數(shù)體現(xiàn)),否則,報(bào)異常
        Date date2 = sdf1.parse("2019-08-09 04:02 13");
        System.out.println(date2);
    }
 /*
    練習(xí)一:字符串"2020-09-08"轉(zhuǎn)換為java.sql.Date
     */
    @Test
    public void testExer() throws ParseException {
        String brith = "2020-09-08";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyy-MM-dd");
        Date date = sdf1.parse(brith);
        java.sql.Date brithDate = new java.sql.Date(date.getTime());
        System.out.println(brithDate);
    }

 /*
    Calendar日歷類(抽象類)的使用
     */
    @Test
    public void testCalendar(){
        //1.實(shí)例化
        //方式一:創(chuàng)建其子類(GregorianCalendar)的對(duì)象
        //方式二:調(diào)用其靜態(tài)方法getInstance()
        Calendar calendar = Calendar.getInstance();
        //2.常用方法
        //get()
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
        //set()
        calendar.set(Calendar.DAY_OF_MONTH,22);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        //add()
        calendar.add(Calendar.DAY_OF_MONTH,-3);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        //getTime():日歷類 -->Date
        Date date1 = calendar.getTime();
        calendar.setTime(date1);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
    }

JDK 8中關(guān)于日期和時(shí)間的API測(cè)試

LocalDate 、 LocalTime 、 LocalDateTime 類是其中較重要的幾個(gè)類,它們的實(shí)例是不可變的對(duì)象,分別表示使用 ISO —8601日歷系統(tǒng)的日期、時(shí)間、日期和時(shí)間。它們提供了簡(jiǎn)單的本地日期或時(shí)間,并不包含當(dāng)前的時(shí)間信息,也不包含與時(shí)區(qū)相關(guān)的信息。
> LocalDate 代表 IOS 格式( yyyy - MM - dd )的日期,可以存儲(chǔ)生日、紀(jì)念日等日期。> LocalTime 表示一個(gè)時(shí)間,而不是日期。
> LocalDateTime 是用來(lái)表示日期和時(shí)間的,這是一個(gè)最常用的類之一。
注: ISO —8601日歷系統(tǒng)是圍際標(biāo)準(zhǔn)化組織制定的現(xiàn)代公民的門期和時(shí)間的表示法,也就是公歷。

/*
    LocalDate,LocalTime,LocalDateTime的使用
     */
    public void test1(){
        //now():獲取當(dāng)前的日期,時(shí)間,日期+時(shí)間
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);//2021-08-09
        System.out.println(localTime);//21:19:25.264
        System.out.println(localDateTime);//2021-08-09T21:19:25.264
        //of():設(shè)置指定的年,月,日,時(shí),分,秒.沒(méi)有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2020,10,6,13,23,43);
        System.out.println(localDateTime1);
        //getXxx():獲取相關(guān)的屬性
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMinute());
        //體現(xiàn)不可變性
        //withXxx():設(shè)置相關(guān)的屬性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System.out.println(localDate);
        System.out.println(localDate1);
        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println(localDate);
        System.out.println(localDateTime2);
    }

/*
    Instant的使用
    類似于java.util.Date類
     */
    @Test
    public void test2(){
        //now():獲取本初子午線對(duì)應(yīng)的標(biāo)準(zhǔn)時(shí)間
        Instant instant = Instant.now();
        System.out.println(instant);//2021-08-09T15:08:46.818Z
        //添加時(shí)間的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2021-08-09T23:08:46.818+08:00
        //toEpochMilli():獲取自1970年1月1日0時(shí)0分0秒(UTC)開(kāi)始的毫秒數(shù)-->Date類的getTime()
        long milli = instant.toEpochMilli();
        System.out.println(milli);//1628521726818
        //ofEpochMilli():通過(guò)給定的毫秒數(shù),獲取Instant實(shí)例-->Date(long millis)
        Instant instant1 = Instant.ofEpochMilli(12243455253L);
        System.out.println(instant1);//1970-05-22T16:57:35.253Z
    }
  /*
    DateTimeFormatter:格式化或解析日期\時(shí)間
    類似于SimpleDateFormat
     */
    @Test
    public void test3(){
//        方式一:預(yù)定義的標(biāo)準(zhǔn)格式,如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期-->字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String str1 = formatter.format(localDateTime);
        System.out.println(localDateTime);//2021-08-09T23:43:52.820
        System.out.println(str1);//2021-08-09T23:43:52.82
        //解析:字符串-->日期
//        TemporalAccessor parse = formatter.parse(" 2021-08-09T23:43:52.82");
//        System.out.println(parse);
//        方式二:本地化相關(guān)的格式,如:ofLocalizedDateTime
//        FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT:適用于LocalDateTime
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        //格式化:
        String str2 = formatter1.format(localDateTime);
        System.out.println(str2);//2021年8月10日 上午10時(shí)32分48秒
//        本地化相關(guān)的格式,如:ofLocalizedDate()
//        FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT:適用于LocalDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        //格式化
        String str3 = formatter2.format(LocalDate.now());
        System.out.println(str3);//2021-8-10
 
 
//        重點(diǎn)?。。。。。?!:方式三:自定義的格式.如:ofPattern("yyyy-MM-dd hh:mm:ss")
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String str4 = formatter3.format(LocalDateTime.now());
        System.out.println(str4);//2021-08-10 10:53:40
        //解析
        TemporalAccessor accessor = formatter3.parse("2021-08-10 10:53:40");
        System.out.println(accessor);//{MinuteOfHour=53, NanoOfSecond=0, MicroOfSecond=0, HourOfAmPm=10, SecondOfMinute=40, MilliOfSecond=0},ISO resolved to 2021-08-10
    }

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

相關(guān)文章

  • 深入淺出探索Java分布式鎖原理

    深入淺出探索Java分布式鎖原理

    單體系統(tǒng)中,在高并發(fā)場(chǎng)景下想要訪問(wèn)共享資源的時(shí)候,我們需要通過(guò)加鎖的方式來(lái)保證共享資源并發(fā)的安全性,確保在同一時(shí)刻只有一個(gè)線程對(duì)共享資源進(jìn)行操作
    2022-02-02
  • 淺談將JNI庫(kù)打包入jar文件

    淺談將JNI庫(kù)打包入jar文件

    這篇文章主要介紹了淺談將JNI庫(kù)打包入jar文件,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Spring?Cloud?Gateway中netty線程池優(yōu)化示例詳解

    Spring?Cloud?Gateway中netty線程池優(yōu)化示例詳解

    這篇文章主要介紹了Spring?Cloud?Gateway中netty線程池優(yōu)化示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • HashMap方法之Map.getOrDefault()解讀及案例

    HashMap方法之Map.getOrDefault()解讀及案例

    這篇文章主要介紹了HashMap方法之Map.getOrDefault()解讀及案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java使用easyExcel導(dǎo)出excel數(shù)據(jù)案例

    Java使用easyExcel導(dǎo)出excel數(shù)據(jù)案例

    這篇文章主要介紹了Java使用easyExcel導(dǎo)出excel數(shù)據(jù)案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • SpringBoot整合JdbcTemplate的示例代碼

    SpringBoot整合JdbcTemplate的示例代碼

    JdbcTemplate是Spring框架自帶的對(duì)JDBC操作的封裝,目的是提供統(tǒng)一的模板方法使對(duì)數(shù)據(jù)庫(kù)的操作更加方便、友好,效率也不錯(cuò),這篇文章主要介紹了SpringBoot整合JdbcTemplate,需要的朋友可以參考下
    2022-08-08
  • Mybatis通過(guò)Mapper代理連接數(shù)據(jù)庫(kù)的方法

    Mybatis通過(guò)Mapper代理連接數(shù)據(jù)庫(kù)的方法

    這篇文章主要介紹了Mybatis通過(guò)Mapper代理連接數(shù)據(jù)庫(kù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-11-11
  • Java連接六類數(shù)據(jù)庫(kù)技巧全攻略

    Java連接六類數(shù)據(jù)庫(kù)技巧全攻略

    本文主要為大家介紹了Java與Oracle、DB2、Sql Server、Sybase、MySQL、PostgreSQL等數(shù)據(jù)庫(kù)連接的方法。
    2015-09-09
  • springBoot解決static和@Component遇到的bug

    springBoot解決static和@Component遇到的bug

    這篇文章主要介紹了springBoot解決static和@Component遇到的bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java基礎(chǔ)教程之構(gòu)造器與方法重載

    Java基礎(chǔ)教程之構(gòu)造器與方法重載

    這篇文章主要介紹了Java基礎(chǔ)教程之構(gòu)造器與方法重載,構(gòu)造器可以初始化數(shù)據(jù)成員,還可以規(guī)定特定的操作,本文還對(duì)方法重載做了介紹,需要的朋友可以參考下
    2014-08-08

最新評(píng)論

浪卡子县| 永胜县| 揭阳市| 丹东市| 彭山县| 通河县| 宜黄县| 新巴尔虎右旗| 巩义市| 云霄县| 西乌珠穆沁旗| 融水| 栾川县| 长岭县| 凭祥市| 庆安县| 江达县| 长岛县| 顺平县| 平果县| 前郭尔| 宁德市| 隆昌县| 景东| 孟州市| 思南县| 高尔夫| 张家港市| 扎囊县| 揭西县| 潜山县| 惠来县| 杨浦区| 曲沃县| 米林县| 彩票| 揭西县| 东丽区| 内乡县| 尉犁县| 西华县|