获取指定月 获取指定日期是星期几
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | public static List<ExcelDateHead> getByMonth(String date){ DateFormat dateFormat= new SimpleDateFormat( "yyyy-MM" ); Calendar c = Calendar.getInstance(); try { c.setTime(dateFormat.parse(date)); } catch (ParseException e) { // TODO: handle exception return null ; } int firstDay=c.getMinimum(Calendar.DAY_OF_MONTH); int endDay=c.getActualMaximum(Calendar.DAY_OF_MONTH); List<ExcelDateHead> list= new ArrayList<ExcelDateHead>(); for ( int i = firstDay; i <= endDay; i++) { c.set(Calendar.DAY_OF_MONTH, i); //c.add(Calendar.DAY_OF_MONTH, 1); String dayOfWeek=getWeekOfDate(c.getTime()); int weekint=c.get(Calendar.WEEK_OF_MONTH); String weeks= "" ; weeks=getweekOfMonth(weekint); String day=String.valueOf(i); int index=weekOfExist(weeks,list); if (index==- 1 ){ ExcelDateHead excelDateHead= new ExcelDateHead(); excelDateHead.setWeekOfMonth(weeks); ExcelDateBody excelDateBody= new ExcelDateBody(); excelDateBody.setDay(day); excelDateBody.setWeek(dayOfWeek); excelDateBody.setDetail(c.getTime()); excelDateHead.getBody().add(excelDateBody); list.add(excelDateHead); } else { ExcelDateBody excelDateBody= new ExcelDateBody(); excelDateBody.setWeek(dayOfWeek); excelDateBody.setDay(day); excelDateBody.setDetail(c.getTime()); list.get(index).getBody().add(excelDateBody); } } return list; } /** * 获取指定日期是星期几 * 参数为null时表示获取当前日期是星期几 * @param date * @return */ public static String getWeekOfDate(Date date) { String[] weekOfDays = { "日" , "一" , "二" , "三" , "四" , "五" , "六" }; Calendar calendar = Calendar.getInstance(); if (date != null ){ calendar.setTime(date); } int w = calendar.get(Calendar.DAY_OF_WEEK) - 1 ; if (w < 0 ){ w = 0 ; } return weekOfDays[w]; } public static String getweekOfMonth( int week){ String[] weeks={ "第一周" , "第二周" , "第三周" , "第四周" , "第五周" , "第六周" }; //System.out.println(week); int a=week- 1 ; if (a< 0 ){ a= 0 ; } return weeks[a]; } public static int weekOfExist(String weeks,List<ExcelDateHead> list){ for ( int i = 0 ; i < list.size(); i++) { if (list.get(i).getWeekOfMonth().equals(weeks)){ return i; } } return - 1 ; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步