public class TimeUtils { //计算今天到本月月初的时间集合 public static List<Date> getDayListOfMonth(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); List<Date> list = new ArrayList<>(); Calendar calender = Calendar.getInstance(); int year = calender.get(Calendar.YEAR);//年份 int month = calender.get(Calendar.MONTH)+1;//月份 int day = Integer.parseInt(sdf.format(calender.getTime()).substring(8)); for (int i = 1; i <= day; i++) { String aDate = String.valueOf(year)+"-"+month+"-"+i; Date parse = null; try { parse = sdf.parse(aDate); } catch (ParseException e) { e.printStackTrace(); } list.add(parse); } return list; } } public static List<String> monthsOfYear(){ //计算过去12个月包含本月 List<String> monthList=new ArrayList<>(); LocalDate today = LocalDate.now(); for(int i = 0;i < 12; i++){ LocalDate localDate = today.minusMonths(i); String month = localDate.toString().substring(0,7); monthList.add(month); } return monthList; }
/** * @Title: getDays * @Description:计算某一年某一月的日期集合 * @author xiaojie * @param year * @param month * @return * @throws */ public static List<Date> getDays(int year, int month) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd"); List<Date> list = new ArrayList<Date>(); Calendar calendar = Calendar.getInstance(); try { calendar.setTime(sdf.parse(year+"-"+month)); int day =calendar.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = 1; i <= day; i++) { String aDate = String.valueOf(year)+"-"+month+"-"+i; Date parse = sdfDay.parse(aDate); list.add(parse); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }
/* * 根据日期获取周几 * @param date * @todo * @author xiaojie * @date 2021/8/3 15:55 * @return java.lang.String */ public static String getWeek(Date date){ SimpleDateFormat sdf = new SimpleDateFormat("EEEE"); String week = sdf.format(date); return week; }