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

詳解Java關于JDK中時間日期的API

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

JDK 8 之前日期和時間的API測試

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

  /*
    java.util.Date類
            |---java.sql.Date類(兩個是子父類的關系)
    1.兩個構(gòu)造器的使用
        >構(gòu)造器一:Date():創(chuàng)建一個對應當前時間的Date對象
        >構(gòu)造器二:創(chuàng)建指定毫秒數(shù)的Date對象
    2.兩個方法的使用
        >toString():顯示當前的年、月、日、時、分、秒
        >getTime():獲取當前Date對象對應的毫秒數(shù)。(時間戳)
    3.java.sql.Date對應著數(shù)據(jù)庫中的日期類型的變量
        >如何實例化
        >如何將java.util.Date對象轉(zhuǎn)換為java.sql.Date對象
     */
    @Test
    public void test2(){
        //構(gòu)造器一:Date():創(chuàng)建一個對應當前時間的Date對象
        Date  date1 = new Date();
        System.out.println(date1.toString());//Sun Aug 08 18:07:27 CST 2021
        //構(gòu)造器二:創(chuàng)建指定毫秒數(shù)的Date對象
        Date  date2 = new Date(1534798293927L);
        System.out.println(date2.toString());//Tue Aug 21 04:51:33 CST 2018
        //創(chuàng)建java.sql.Date對象
        java.sql.Date date3 = new java.sql.Date(237493269533L);
        System.out.println(date3);//1977-07-12
        //如何將java.util.Date對象轉(zhuǎn)換為java.sql.Date對象
        //情況一:(面向?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對日期Date類的格式化和解析
    1.兩個操作:
    1.1 格式化:日期--->字符串
    1.2 解析:格式化的逆過程:字符串--->日期
    2.SimpleDateFormat的實例化
     */
    @Test
    public void testSimpleDateFormat() throws ParseException {
        //實例化SimpleDateFormat
        SimpleDateFormat sdf = new SimpleDateFormat();
        //格式化:日期--->字符串
        Date date = new Date();
        System.out.println(date);
        String format = sdf.format(date);
        System.out.println(format);
        //解析:格式化的逆過程,字符串--->日期
        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識別的格式(通過構(gòu)造器參數(shù)體現(xiàn)),否則,報異常
        Date date2 = sdf1.parse("2019-08-09 04:02 13");
        System.out.println(date2);
    }
 /*
    練習一:字符串"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.實例化
        //方式一:創(chuàng)建其子類(GregorianCalendar)的對象
        //方式二:調(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中關于日期和時間的API測試

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

/*
    LocalDate,LocalTime,LocalDateTime的使用
     */
    public void test1(){
        //now():獲取當前的日期,時間,日期+時間
        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():設置指定的年,月,日,時,分,秒.沒有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2020,10,6,13,23,43);
        System.out.println(localDateTime1);
        //getXxx():獲取相關的屬性
        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():設置相關的屬性
        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():獲取本初子午線對應的標準時間
        Instant instant = Instant.now();
        System.out.println(instant);//2021-08-09T15:08:46.818Z
        //添加時間的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2021-08-09T23:08:46.818+08:00
        //toEpochMilli():獲取自1970年1月1日0時0分0秒(UTC)開始的毫秒數(shù)-->Date類的getTime()
        long milli = instant.toEpochMilli();
        System.out.println(milli);//1628521726818
        //ofEpochMilli():通過給定的毫秒數(shù),獲取Instant實例-->Date(long millis)
        Instant instant1 = Instant.ofEpochMilli(12243455253L);
        System.out.println(instant1);//1970-05-22T16:57:35.253Z
    }
  /*
    DateTimeFormatter:格式化或解析日期\時間
    類似于SimpleDateFormat
     */
    @Test
    public void test3(){
//        方式一:預定義的標準格式,如: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);
//        方式二:本地化相關的格式,如: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時32分48秒
//        本地化相關的格式,如: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
 
 
//        重點?。。。。。?!:方式三:自定義的格式.如: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
    }

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

相關文章

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

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

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

    淺談將JNI庫打包入jar文件

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

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

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

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

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

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

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

    SpringBoot整合JdbcTemplate的示例代碼

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

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

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

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

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

    springBoot解決static和@Component遇到的bug

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

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

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

最新評論

梁山县| 措美县| 芦山县| 平湖市| 泾源县| 凤山市| 金昌市| 开江县| 桦川县| 驻马店市| 读书| 清水县| 牙克石市| 昌都县| 德保县| 岳西县| 阿拉善盟| 江门市| 丹巴县| 秦安县| 广元市| 满洲里市| 会同县| 石嘴山市| 清河县| 遵义县| 大荔县| 宜川县| 敦化市| 南丰县| 胶南市| 南阳市| 云阳县| 来安县| 奇台县| 峨眉山市| 自贡市| 南川市| 临泽县| 承德县| 青铜峡市|