/** * 某一个月第一天和最后一天 * @param number * @return */ public static Map<String, String> getBeforeMonthDate(int number) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -number); Map map = getFirstday_Lastday_Month(cal.getTime()); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT2); return map; } private static Map<String, String> getFirstday_Lastday_Month(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); Date theDate = calendar.getTime(); //上个月第一天 GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance(); gcLast.setTime(theDate); gcLast.set(Calendar.DAY_OF_MONTH, 1); String day_first = df.format(gcLast.getTime()); StringBuffer str = new StringBuffer().append(day_first).append(" 00:00:00"); day_first = str.toString(); //上个月最后一天 calendar.add(Calendar.MONTH, 1); //加一个月 calendar.set(Calendar.DATE, 1); //设置为该月第一天 calendar.add(Calendar.DATE, -1); //再减一天即为上个月最后一天 String day_last = df.format(calendar.getTime()); StringBuffer endStr = new StringBuffer().append(day_last).append(" 23:59:59"); day_last = endStr.toString(); Map<String, String> map = new HashMap<String, String>(); map.put("first", day_first); map.put("last", day_last); return map; } /** * 得到本日的前几年时间 * * @return */ public static Map getDateBeforeYear(int number) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, -number); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT2); int year = Integer.parseInt(sdf.format(cal.getTime()).substring(0,4)); String first = getYearFirst(year); String last = getYearLast(year); Map<String, String> map = new HashMap<String, String>(); map.put("first", first); map.put("last", last); return map; } /** * 获取某年第一天日期 * @param year 年份 * @return Date */ public static String getYearFirst(int year){ Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, year); Date currYearFirst = calendar.getTime(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT2); return sdf.format(currYearFirst); } /** * 获取某年最后一天日期 * @param year 年份 * @return Date */ public static String getYearLast(int year){ Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR,year); calendar.roll(Calendar.DAY_OF_YEAR, -1); Date currYearLast = calendar.getTime();SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT2); return sdf.format(currYearLast); }