亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java天數(shù)計算函數(shù)(當(dāng)前月天數(shù)、某月總天數(shù)及某月剩余天數(shù))4種方法實現(xiàn)代碼

 更新時間:2023年10月12日 10:34:41   作者:左眼看成愛  
日常開發(fā)中會遇到關(guān)于日期的計算,比如當(dāng)月的天數(shù)、兩日期之間的天數(shù)、當(dāng)月剩余天數(shù)等等,這篇文章主要給大家介紹了關(guān)于java天數(shù)計算函數(shù)(當(dāng)前月天數(shù)、某月總天數(shù)及某月剩余天數(shù))4種方法實現(xiàn)的相關(guān)資料,需要的朋友可以參考下

1,Java8 LocalDate

    public static int getDaysOfMonth(String dateStr) {
        LocalDate date = LocalDate.parse(dateStr + "-01");
        return date.lengthOfMonth();
    }

2,利用日歷函數(shù)

    public static int getDaysOfMonth(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(dateStr));
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

3,也是利用日歷函數(shù),但是不需要 throws ParseException

 
    /**
     * 獲取日期當(dāng)月的天數(shù)
     * @param dateStr yyyy-MM 或者yyyy-MM-dd
     * */
    public static int getDaysByDate(String dateStr){
        int year= Integer.parseInt(dateStr.substring(0,4));
        int month=Integer.parseInt(dateStr.substring(5,7));
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0);
        return c.get(Calendar.DAY_OF_MONTH);
    }

測試樣例代碼: 

        String dateStr = "2023-01";
        int days = getDaysOfMonth(dateStr);
        System.out.println(dateStr + " has " + days + " days.");

4,當(dāng)月剩余天數(shù)

 
    /**
     * 當(dāng)月剩余天數(shù)
     * @param date 格式y(tǒng)yyy-MM-dd
     * */
    public static Integer monthEndNum(String date){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(dateTime);
        int today = c.get(Calendar.DAY_OF_MONTH);
        int last = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        return last - today;
    }
  public static void main(String[] args) {
        Integer days = monthEndNum("2023-01-20");
        System.out.println("2023年1月剩余天數(shù):"+days);
    }

5,獲取當(dāng)前月天數(shù)

    //獲取當(dāng)前月的天數(shù)
    public static int getDaysOfCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

測試代碼:

    public static void main(String[] args) throws ParseException {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        System.out.println(dateString);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");
        String dateString2 = dateFormat.format(currentTime);
        System.out.println(dateString2);
        Date date1 = dateFormat.parse(dateString2);
        String dateString3 = formatter.format(date1);
        System.out.println(dateString3);
        // 將日期字符串轉(zhuǎn)換為LocalDate對象
        LocalDate date = LocalDate.parse("2023-04-06");
        // 將LocalDate對象格式化為指定格式的字符串
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy年M月d日");
        String formattedDate = date.format(formatter1);
        System.out.println(formattedDate);
        // 將格式化后的字符串轉(zhuǎn)換為LocalDate對象
        LocalDate parsedDate = LocalDate.parse(formattedDate, formatter1);
        // 將LocalDate對象轉(zhuǎn)換為指定格式的字符串
        String parsedDateString = parsedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(parsedDateString);
        // 確認(rèn)兩個日期字符串相等
        assert parsedDateString.equals("2023-04-06");
        // 確認(rèn)兩個LocalDate對象相等
        assert parsedDate.equals(date);
        System.out.println("---------------------");
        String sdate="2023-04-06";
        String sdate2="2023年4月6日";
        String sdate3="2023-04";
        String sdate4="2023年4月";
        System.out.println(convertDateToChs(sdate));
        System.out.println(convertToChinese(sdate));
        System.out.println((convertDateToEn(sdate2)));
        System.out.println((convertDateToEnglish(sdate2)));
        System.out.println((convertToShortChinese(sdate3)));
        System.out.println((convertToShortEnglish(sdate4)));
        String dateStr = "2023-02";
        int days = getDaysByDate(dateStr);
        System.out.println(dateStr + " has " + days + " days.");
        System.out.println("今年有" +   getDaysInCurrentYear() + " days.");
        System.out.println("今年已過去" +   getDaysPassedInCurrentYear() + " days.");
        System.out.println("今年剩余" +   getDaysEndInCurrentYear() + " days.");
    }

測試效果截圖:

總結(jié)

到此這篇關(guān)于java天數(shù)計算函數(shù)(當(dāng)前月天數(shù)、某月總天數(shù)及某月剩余天數(shù))4種方法實現(xiàn)的文章就介紹到這了,更多相關(guān)java天數(shù)計算函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深入理解Java原生的序列化機制

    深入理解Java原生的序列化機制

    Java 提供了一種對象序列化的機制,該機制中,一個對象可以被表示為一個字節(jié)序列,該字節(jié)序列包括該對象的數(shù)據(jù)、有關(guān)對象的類型的信息和存儲在對象中數(shù)據(jù)的類型。下面小編和大家來一起學(xué)習(xí)一下吧
    2019-06-06
  • spring集成httpclient配置的詳細過程

    spring集成httpclient配置的詳細過程

    spring框架是一個非常強大的框架這里就不多說了,那么主要是介紹spring與httpclient的整合集成過程,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • Java實現(xiàn)滑動窗口算法的示例代碼

    Java實現(xiàn)滑動窗口算法的示例代碼

    滑動窗口算法是一種高效解決子數(shù)組、子字符串問題的算法,廣泛應(yīng)用于數(shù)據(jù)流處理、網(wǎng)絡(luò)限流和字符串操作等場景,本文將詳細解析滑動窗口算法的核心思想、常見問題及其實現(xiàn)方式,需要的朋友可以參考下
    2025-03-03
  • java使用定時器實現(xiàn)監(jiān)聽數(shù)據(jù)變化

    java使用定時器實現(xiàn)監(jiān)聽數(shù)據(jù)變化

    這篇文章主要為大家詳細介紹了Java如何使用定時器監(jiān)聽數(shù)據(jù)變化,當(dāng)滿足某個條件時(例如沒有數(shù)據(jù)更新)自動執(zhí)行某項任務(wù),有興趣的可以了解下
    2023-11-11
  • Mybatis邏輯分頁與物理分頁PageHelper使用解析

    Mybatis邏輯分頁與物理分頁PageHelper使用解析

    這篇文章主要為大家介紹了Mybatis邏輯分頁與物理分頁PageHelper使用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解

    Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解

    這篇文章主要介紹了Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • Java 使用Calendar計算時間的示例代碼

    Java 使用Calendar計算時間的示例代碼

    這篇文章主要介紹了Java 使用Calendar計算時間的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • java實現(xiàn)靜默加載Class示例代碼

    java實現(xiàn)靜默加載Class示例代碼

    這篇文章主要給大家介紹了關(guān)于java實現(xiàn)靜默加載Class的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • 使用java實現(xiàn)“釘釘微應(yīng)用免登進入某H5系統(tǒng)首頁“功能”

    使用java實現(xiàn)“釘釘微應(yīng)用免登進入某H5系統(tǒng)首頁“功能”

    這篇文章主要介紹了用java實現(xiàn)“釘釘微應(yīng)用,免登進入某H5系統(tǒng)首頁“功能”,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • spring cloud實現(xiàn)前端跨域問題的解決方案

    spring cloud實現(xiàn)前端跨域問題的解決方案

    這篇文章主要介紹了 spring cloud實現(xiàn)前端跨域問題的解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01

最新評論