java天數(shù)計算函數(shù)(當(dāng)前月天數(shù)、某月總天數(shù)及某月剩余天數(shù))4種方法實現(xià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使用定時器實現(xiàn)監(jiān)聽數(shù)據(jù)變化
這篇文章主要為大家詳細介紹了Java如何使用定時器監(jiān)聽數(shù)據(jù)變化,當(dāng)滿足某個條件時(例如沒有數(shù)據(jù)更新)自動執(zhí)行某項任務(wù),有興趣的可以了解下2023-11-11Mybatis邏輯分頁與物理分頁PageHelper使用解析
這篇文章主要為大家介紹了Mybatis邏輯分頁與物理分頁PageHelper使用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解
這篇文章主要介紹了Josephus環(huán)的四種解法(約瑟夫環(huán))基于java詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09使用java實現(xiàn)“釘釘微應(yīng)用免登進入某H5系統(tǒng)首頁“功能”
這篇文章主要介紹了用java實現(xiàn)“釘釘微應(yīng)用,免登進入某H5系統(tǒng)首頁“功能”,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10spring cloud實現(xiàn)前端跨域問題的解決方案
這篇文章主要介紹了 spring cloud實現(xiàn)前端跨域問題的解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01