欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2023年10月12日 10:34:41   作者:左眼看成愛(ài)  
日常開(kāi)發(fā)中會(huì)遇到關(guān)于日期的計(jì)算,比如當(dāng)月的天數(shù)、兩日期之間的天數(shù)、當(dāng)月剩余天數(shù)等等,這篇文章主要給大家介紹了關(guān)于java天數(shù)計(jì)算函數(shù)(當(dāng)前月天數(shù)、某月總天數(shù)及某月剩余天數(shù))4種方法實(shí)現(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);
    }

測(cè)試樣例代碼: 

        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);
    }

測(cè)試代碼:

    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時(shí)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)換為L(zhǎng)ocalDate對(duì)象
        LocalDate date = LocalDate.parse("2023-04-06");
        // 將LocalDate對(duì)象格式化為指定格式的字符串
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy年M月d日");
        String formattedDate = date.format(formatter1);
        System.out.println(formattedDate);
        // 將格式化后的字符串轉(zhuǎn)換為L(zhǎng)ocalDate對(duì)象
        LocalDate parsedDate = LocalDate.parse(formattedDate, formatter1);
        // 將LocalDate對(duì)象轉(zhuǎn)換為指定格式的字符串
        String parsedDateString = parsedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(parsedDateString);
        // 確認(rèn)兩個(gè)日期字符串相等
        assert parsedDateString.equals("2023-04-06");
        // 確認(rèn)兩個(gè)LocalDate對(duì)象相等
        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("今年已過(guò)去" +   getDaysPassedInCurrentYear() + " days.");
        System.out.println("今年剩余" +   getDaysEndInCurrentYear() + " days.");
    }

測(cè)試效果截圖:

總結(jié)

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

相關(guān)文章

  • 深入理解Java原生的序列化機(jī)制

    深入理解Java原生的序列化機(jī)制

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

    spring集成httpclient配置的詳細(xì)過(guò)程

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

    Java實(shí)現(xiàn)滑動(dòng)窗口算法的示例代碼

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

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

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

    Mybatis邏輯分頁(yè)與物理分頁(yè)P(yáng)ageHelper使用解析

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

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

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

    Java 使用Calendar計(jì)算時(shí)間的示例代碼

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

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

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

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

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

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

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

最新評(píng)論