求一个月有几周 博客分类: java开发
程序员文章站
2024-03-23 23:46:10
...
int year = 2013; int month= 12 ; Calendar c_begin = new GregorianCalendar(); Calendar c_end = new GregorianCalendar(); c_begin.set(year, month - 1, 1); // 当月第一天 c_end.set(year, month, 1); // 当月最后一天 +1 List<Date> objList = new ArrayList<Date>(); Map<String, Object> objMap = new HashMap<String, Object>(); // 计划当前月计划的月份中,对应的有几个周 if (c_begin.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { objList.add(new java.sql.Date(c_begin.getTimeInMillis())); objList.add(new java.sql.Date(c_begin.getTimeInMillis())); }else{ objList.add(new java.sql.Date(c_begin.getTimeInMillis())); } c_begin.add(Calendar.DAY_OF_YEAR, 1); while (c_begin.before(c_end)) { if (c_begin.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY||c_begin.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) { objList.add(new java.sql.Date(c_begin.getTimeInMillis())); } //System.out.println(c_begin.getTimeInMillis()/3600000/24+"---"+c_end.getTimeInMillis()/3600000/24); if(c_begin.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY&&c_begin.getTimeInMillis()/3600000/24+1==c_end.getTimeInMillis()/3600000/24){ objList.add(new java.sql.Date(c_begin.getTimeInMillis())); } c_begin.add(Calendar.DAY_OF_YEAR, 1); } if (objMap != null && objMap.get(String.valueOf(objMap.size())) != null) { c_end.set(year, month, 0); Timestamp c = new Timestamp(c_end.getTimeInMillis()); SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); String ss = dateFm.format(c); objMap.put(String.valueOf(objMap.size()), objMap.get(String.valueOf(objMap.size())) + "&" + ss); } //System.out.println(objList.toString()); for(int i=0;i<objList.size();i=i+2){ objMap.put(String.valueOf((i+2)/2), objList.get(i).toString()+"&"+ objList.get(i+1).toString()); System.out.println(String.valueOf((i+2)/2)+"---"+objList.get(i).toString()+"&"+ objList.get(i+1).toString()); }