月份、季度、周
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author fan */ public class Test { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] args) throws Exception{ System.out.println(getMonthList()); System.out.println(getQuarterList()); System.out.println(getWeekList()); } /** * 获取当前时间向前推12月的时间列表 * */ public static List<Object> getMonthList(){ // 获取当前时间往前推12个月的年-月列表 List<Object> monthList = new ArrayList<Object>(); for(int i=1; i<=12; i++){ Map<String, Object> date = getMonthInfo(i); monthList.add(date.get("month")); } return monthList; } /** * 获取月列表 * @param n * @return */ private static Map<String, Object> getMonthInfo(int n){ Map<String, Object> monthMap = new HashMap<String, Object>(); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, -n); SimpleDateFormat sfM = new SimpleDateFormat("yyyy-MM"); monthMap.put("month", sfM.format(calendar.getTime())); calendar.set(Calendar.DAY_OF_MONTH, 1); monthMap.put("start", dateFormat.format(calendar.getTime())); calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE)); monthMap.put("end", dateFormat.format(calendar.getTime())); return monthMap; } /** * 获取当前时间向前推4个季度的时间列表 * */ public static List<Object> getQuarterList(){ List<Object> quarterList = new ArrayList<Object>(); for(int i=1; i<=4; i++){ Map<String, Object> date = getQuarterInfo(i); quarterList.add(date.get("year").toString()+"年"+date.get("quarter")); } return quarterList; } /** * 从当前时间往前推几个季度 * n:正数:向前推几个季度;负数向后推几个季度 * @return * 例如:当前时间是2016-12-23,则getQuarterInfo(1)==>{start=2016-07-01, year=2016, end=2016-09-30, quarter=第三季度} * getQuarterInfo(2)==>{start=2016-04-01, year=2016, end=2016-06-30, quarter=第二季度} * getQuarterInfo(3)==>{start=2016-01-01, year=2016, end=2016-03-31, quarter=第一季度} * getQuarterInfo(4)==>{start=2015-10-01, year=2015, end=2015-12-31, quarter=第四季度} * getQuarterInfo(-1)==>{start=2017-01-01, year=2017, end=2017-03-31, quarter=第一季度} */ public static Map<String, Object> getQuarterInfo(int n) { Calendar startCalendar = Calendar.getInstance(); startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - n) * 3); startCalendar.set(Calendar.DAY_OF_MONTH, 1); Calendar endCalendar = Calendar.getInstance(); endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - n) * 3 + 2); endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 存储季度信息 Map<String, Object> quarterMap = new HashMap<String, Object>(); // 季度 switch(startCalendar.get(Calendar.MONTH) + 1){ case 1 : quarterMap.put("quarter", "第一季度"); break; case 4 : quarterMap.put("quarter", "第二季度"); break; case 7 : quarterMap.put("quarter", "第三季度"); break; case 10 : quarterMap.put("quarter", "第四季度"); } // 年份 quarterMap.put("year", startCalendar.get(Calendar.YEAR)); // 季度开始时间(天) quarterMap.put("start", dateFormat.format(startCalendar.getTime())); // 季度结束时间(天) quarterMap.put("end", dateFormat.format(endCalendar.getTime())); return quarterMap; } public static List<Object> getWeekList(){ List<Object> weekList = new ArrayList<Object>(); for(int i=1; i<=52; i++){ Map<String, Object> week = getWeekInfo(i); weekList.add(week.get("year").toString()+"年"+week.get("week").toString()+"周"); } return weekList; } /** * 向前推多少周正数向前,负数向后 * @param n */ public static Map<String, Object> getWeekInfo(int n){ Map<String, Object> weekMap = new HashMap<String, Object>(); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); if(1 == calendar.get(Calendar.DAY_OF_WEEK)) { calendar.add(Calendar.DAY_OF_MONTH, -1); } // 设置周一为第一天 calendar.setFirstDayOfWeek(Calendar.MONDAY); calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek() - calendar.get(Calendar.DAY_OF_WEEK) - 7 * n); weekMap.put("week", calendar.get(Calendar.WEEK_OF_YEAR)); weekMap.put("start", dateFormat.format(calendar.getTime())); calendar.add(Calendar.DATE, 6); weekMap.put("year", calendar.get(Calendar.YEAR)); weekMap.put("end", dateFormat.format(calendar.getTime())); return weekMap; } }