根据年月,获取当月所有日期 public static void main(String[] args) { System.err.println(getMonthDate("2022-07")); } public static List getMonthDate(String timeStamp){ List list = new ArrayList(); String[] split = timeStamp.split("-"); String year = split[0]; String month = split[1]; Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, Integer.parseInt(year)); calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1); int day = calendar.getActualMaximum(Calendar.DATE); for (int i = 1; i <= day; i++) { String days = ""; if (i<10){ days = "0"+i; }else { days = String.valueOf(i); } String aDate = year+"-"+month+"-"+days; list.add(aDate); } return list; }
根据年月,获取当月所有周末 public static List<String> getWeekend(String timeStamp) { List list = new ArrayList(); String[] split = timeStamp.split("-"); String year = split[0]; String month = split[1]; Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, Integer.parseInt(year)); calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1); // 设置为当月第一天 calendar.set(Calendar.DAY_OF_MONTH, 0); // 当月最大天数 int daySize = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = 0; i < daySize - 1; i++) { String days = ""; //在第一天的基础上加1 calendar.add(Calendar.DATE, 1); int week = calendar.get(Calendar.DAY_OF_WEEK); // 1代表周日,7代表周六 判断这是一个星期的第几天从而判断是否是周末 if (week == Calendar.SATURDAY || week == Calendar.SUNDAY) { int ct = calendar.get(Calendar.DAY_OF_MONTH); if (ct < 10) { days = "0" + ct; } else { days = String.valueOf(ct); } // 得到当天是一个月的第几天 list.add(year + "-" + month + "-" + days); } } return list; }
标签:java,String,int,周末,当月,split,Calendar,calendar From: https://www.cnblogs.com/Objecting/p/16850402.html