java天数计算函数(当前月天数、某月总天数及某月剩余天数)4种方法实现代码
作者:左眼看成爱
日常开发中会遇到关于日期的计算,比如当月的天数、两日期之间的天数、当月剩余天数等等,这篇文章主要给大家介绍了关于java天数计算函数(当前月天数、某月总天数及某月剩余天数)4种方法实现的相关资料,需要的朋友可以参考下
1,Java8 LocalDate
public static int getDaysOfMonth(String dateStr) { LocalDate date = LocalDate.parse(dateStr + "-01"); return date.lengthOfMonth(); }
2,利用日历函数
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,也是利用日历函数,但是不需要 throws ParseException
/** * 获取日期当月的天数 * @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,当月剩余天数
/** * 当月剩余天数 * @param date 格式yyyy-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月剩余天数:"+days); }
5,获取当前月天数
//获取当前月的天数 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); // 将日期字符串转换为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); // 将格式化后的字符串转换为LocalDate对象 LocalDate parsedDate = LocalDate.parse(formattedDate, formatter1); // 将LocalDate对象转换为指定格式的字符串 String parsedDateString = parsedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); System.out.println(parsedDateString); // 确认两个日期字符串相等 assert parsedDateString.equals("2023-04-06"); // 确认两个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."); }
测试效果截图:
总结
到此这篇关于java天数计算函数(当前月天数、某月总天数及某月剩余天数)4种方法实现的文章就介绍到这了,更多相关java天数计算函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!