因领导懒惰,页面查询条件中的日期项懒得手工输入,查询条件变作上月,上季,上周,故讨教多方,写了一个工具类,以作日后参考. package util; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DateTool { public static long ONE_DAYS_TIME = 24*60*60*1000;//1天的毫秒数 public static long TWO_DAYS_TIME = 2*24*60*60*1000;//2天的毫秒数 public static long THREE_DAYS_TIME = 3*24*60*60*1000;//3天的毫秒数 public static long FOUR_DAYS_TIME = 4*24*60*60*1000;//4天的毫秒数 public static long FIVE_DAYS_TIME = 5*24*60*60*1000;//5天的毫秒数 public static long SIX_DAYS_TIME = 6*24*60*60*1000;//6天的毫秒数 public static long SEVEN_DAYS_TIME = 7*24*60*60*1000;//7天的毫秒数 public static long EIGHT_DAYS_TIME = 8*24*60*60*1000;//8天的毫秒数 public static long NINE_DAYS_TIME = 9*24*60*60*1000;//9天的毫秒数 public static long TEN_DAYS_TIME = 10*24*60*60*1000;//10天的毫秒数 public static long ELEVEN_DAYS_TIME = 11*24*60*60*1000;//11天的毫秒数 public static long TWELVE_DAYS_TIME = 12*24*60*60*1000;//12天的毫秒数 public static long THIRTEEN_DAYS_TIME = 13*24*60*60*1000;//13天的毫秒数 / ** * 得到某个日期上一年的年份 * @param dateString 格式yyyyMMdd * @return 4位年份值 */ public static String getLastYear(String dateString){ String currentYear = dateString.substring(0, 4);//该日期的所在年份 return String.valueOf(Integer.parseInt(currentYear)-1); } / ** * 得到某个日期上月的月份 * @param dateString 格式yyyyMMdd * @return 2位月份值 1月份返回12 */ public static String getLastMonth(String dateString){ String currentMonth = dateString.substring(4, 6);//改日期所在月份 if("01".equals(currentMonth)){ currentMonth = "12"; }else if(Integer.parseInt(currentMonth)>10){ currentMonth = String.valueOf(Integer.parseInt(currentMonth)-1); }else{ currentMonth = "0"+String.valueOf(Integer.parseInt(currentMonth)-1); } return currentMonth; } / ** * 得到某一日期的上个月的开始日期 * @param dateString 格式yyyyMMdd * @return 8位日期值 */ public static String getBeginningDateOfLastMonth(String dateString) { String lastYear = getLastYear(dateString);//该日期上年年份 String lastMonth = getLastMonth(dateString);//改日期上月月份 if("12".equals(lastMonth)){ return lastYear+lastMonth+"01"; }else{ return dateString.substring(0,4)+lastMonth+"01"; } } / ** * 得到某个日期上月月份的结束日期 * @param dateString 格式yyyyMMdd * @return 8位日期值 */ public static String getEndingDateOfLastMonth(String dateString){ String lastYear_month = getBeginningDateOfLastMonth(dateString).substring(0, 6);//该月份上月月份的年+月 String lastYear = lastYear_month.substring(0,4);//该日期上年年份 String lastMonth = lastYear_month.substring(4,6);//该日期上月月份 String date = null; switch (Integer.parseInt(lastMonth)) { case 2: int intValueOfYear = Integer.parseInt(lastYear); if((intValueOfYear%4==0)||(intValueOfYear%100!=0&&intValueOfYear%400==0)){ date = lastYear_month+"29";//闰年2月29天 }else{ date = lastYear_month+"28";//非闰年2月28天 } break; case 4: case 6: case 9: case 11: date = lastYear_month+"30";//4,6,9,11月30天 break; default: date = lastYear_month+"31";//1,3,5,7,8,10,12月31天 break; } return date; } / ** * 得到某个日期上一季度的起始月份 * @param dateString * @return 2位月份值 */ public static String getBeginningMonthOfLastSeason(String dateString){ String currentMonth = dateString.substring(4, 6);//当前月份 String monthOfLastSeason = null;//上一季度起始月份 switch(Integer.parseInt(currentMonth)){ case 1: case 2: case 3: monthOfLastSeason = "10"; break; case 4: case 5: case 6: monthOfLastSeason = "01"; break; case 7: case 8: case 9: monthOfLastSeason = "04"; break; default: monthOfLastSeason = "07"; break; } return monthOfLastSeason; } / ** * 得到某个日期上一季度的结束月份 * @param dateString * @return 2位月份值 */ public static String getEndingMonthOfLastSeason(String dateString){ String currentMonth = dateString.substring(4, 6);//当前月份 String monthOfLastSeason = null;//上一季度结束月份 switch(Integer.parseInt(currentMonth)){ case 1: case 2: case 3: monthOfLastSeason = "12"; break; case 4: case 5: case 6: monthOfLastSeason = "03"; break; case 7: case 8: case 9: monthOfLastSeason = "06"; break; default: monthOfLastSeason = "09"; break; } return monthOfLastSeason; } / ** * 得到某个日期上一季度的开始日期 * @param dateString * @return 8位日期 */ public static String getBeginingDateOfLastSeason(String dateString){ String currentYear = dateString.substring(0, 4);//该日期所在年份 String lastYear = getLastYear(dateString);//该日期上一年年份 String currentMonth = dateString.substring(4, 6);//该日期所在月份 String beginningMonthOfLastSeason = getBeginningMonthOfLastSeason(dateString);//该日期上一季度起始月份 String date = null; if("01".equals(currentMonth)||"02".equals(currentMonth)||"03".equals(currentMonth)){ date = lastYear+beginningMonthOfLastSeason+"01"; }else{ date = currentYear+beginningMonthOfLastSeason+"01"; } return date; } / ** * 得到某个日期上一季度的结束日期 * @param dateString * @return 8位日期 */ public static String getEndingDateOfLastSeason(String dateString){ String currentYear = dateString.substring(0, 4);//该日期所在年份 String lastYear = getLastYear(dateString);//该日期上一年年份 String currentMonth = dateString.substring(4, 6);//该日期所在月份 String endingMonthOfLastSeason = getEndingMonthOfLastSeason(dateString);//该日期上季度结束月份 String date = null; if("01".equals(currentMonth)||"02".equals(currentMonth)||"03".equals(currentMonth)){ switch (Integer.parseInt(endingMonthOfLastSeason)) { case 2: int intValueOfYear = Integer.parseInt(lastYear); if((intValueOfYear%4==0)||(intValueOfYear%100!=0&&intValueOfYear%400==0)){ date = lastYear+endingMonthOfLastSeason+"29";//闰年2月29天 }else{ date = lastYear+endingMonthOfLastSeason+"28";//非闰年2月28天 } break; case 4: case 6: case 9: case 11: date = lastYear+endingMonthOfLastSeason+"30";//4,6,9,11月30天 break; default: date = lastYear+endingMonthOfLastSeason+"31";//1,3,5,7,8,10,12月31天 break; } }else{ switch (Integer.parseInt(endingMonthOfLastSeason)) { case 2: int intValueOfYear = Integer.parseInt(currentYear); if((intValueOfYear%4==0)||(intValueOfYear%100!=0&&intValueOfYear%400==0)){ date = currentYear+endingMonthOfLastSeason+"29";//闰年2月29天 }else{ date = currentYear+endingMonthOfLastSeason+"28";//非闰年2月28天 } break; case 4: case 6: case 9: case 11: date = currentYear+endingMonthOfLastSeason+"30";//4,6,9,11月30天 break; default: date = currentYear+endingMonthOfLastSeason+"31";//1,3,5,7,8,10,12月31天 break; } } return date; } / ** * 得到某个日期上一周的起始日期(大致思路:获得该日期的星期信息,和距离1970.1.1的毫秒数,如为周一,将此毫秒数向前减7天的毫秒数,周二-周日依次类推) * @param dateString * @return 8位日期 * @throws ParseException */ public static String getBeginningDateOfLastWeek(String dateString) throws ParseException{ DateFormat dateFormater = new SimpleDateFormat("yyyyMMdd"); Date dateObject = dateFormater.parse(dateString);//将日期字符串转换为日期对象 Calendar calendar = new GregorianCalendar(); calendar.setTime(dateObject); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);//该日期的星期 long time = dateObject.getTime(); switch (dayOfWeek) { case 2: dateObject.setTime(time-DateTool.SEVEN_DAYS_TIME);//周一,美国和欧洲习惯把周日当作每周的第一天,故周一为2 break; case 3: dateObject.setTime(time-DateTool.EIGHT_DAYS_TIME); break; case 4: dateObject.setTime(time-DateTool.NINE_DAYS_TIME); break; case 5: dateObject.setTime(time-DateTool.TEN_DAYS_TIME); break; case 6: dateObject.setTime(time-DateTool.ELEVEN_DAYS_TIME); break; case 7: dateObject.setTime(time-DateTool.TWELVE_DAYS_TIME); break; default: dateObject.setTime(time-DateTool.THIRTEEN_DAYS_TIME); break; } String date = dateFormater.format(dateObject); return date; } / ** * 得到某个日期上一周的结束日期 * @param dateString * @return 8位日期 * @throws ParseException */ public static String getEndingDateOfLastWeek(String dateString) throws ParseException{ DateFormat dateFormater = new SimpleDateFormat("yyyyMMdd"); Date dateObject = dateFormater.parse(dateString);//将日期字符串转换为日期对象 Calendar calendar = new GregorianCalendar(); calendar.setTime(dateObject); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);//该日期的星期 long time = dateObject.getTime(); switch (dayOfWeek) { case 2: dateObject.setTime(time-DateTool.ONE_DAYS_TIME);//周一,美国和欧洲习惯把周日当作每周的第一天,故周一为2 break; case 3: dateObject.setTime(time-DateTool.TWO_DAYS_TIME); break; case 4: dateObject.setTime(time-DateTool.THREE_DAYS_TIME); break; case 5: dateObject.setTime(time-DateTool.FOUR_DAYS_TIME); break; case 6: dateObject.setTime(time-DateTool.FIVE_DAYS_TIME); break; case 7: dateObject.setTime(time-DateTool.SIX_DAYS_TIME); break; default: dateObject.setTime(time-DateTool.SEVEN_DAYS_TIME); break; } String date = dateFormater.format(dateObject); return date; } }