java天数计算函数(当前月天数、某月总天数、某月剩余天数)4种方法实现
短信预约 -IT技能 免费直播动态提醒
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
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,当月剩余天数
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."); }
测试效果截图:
来源地址:https://blog.csdn.net/wh445306/article/details/130642759
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341