1 package com.mall.common; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.ArrayList; 6 import java.util.Calendar; 7 import java.util.Date; 8 import java.util.List; 9 10 import org.apache.log4j.Logger; 11 12 13 public class DateUtils { 14 private static Logger log=Logger.getLogger(DateUtils.class); 15 16 /** 17 * 获取昨天日期 18 * 19 * @param date 20 * @return 21 */ 22 public static String getYesterDay(Date date) { 23 24 java.text.Format formatter = new java.text.SimpleDateFormat( 25 "yyyy-MM-dd"); 26 27 long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60; 28 29 date.setTime(beforeTime * 1000); 30 31 return formatter.format(date); 32 33 } 34 35 /** 36 * 获取昨天日期(返回数值) 37 * 38 * @param date 39 * @return num 40 */ 41 public static Integer getYesterDayNum(Date date) { 42 43 java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd"); 44 45 long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60; 46 47 date.setTime(beforeTime * 1000); 48 49 return Integer.parseInt(formatter.format(date)); 50 51 } 52 53 /** 54 * 获取一周前的日期(当前日期往前推7天) 55 * 56 * @param date 57 * @return 58 */ 59 public static String getWeekdayBeforeDate(Date date) { 60 61 java.text.Format formatter = new java.text.SimpleDateFormat( 62 "yyyy-MM-dd"); 63 64 long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 7; 65 66 date.setTime(beforeTime * 1000); 67 68 return formatter.format(date); 69 70 } 71 72 /** 73 * 获取一周前的日期(当前日期往前推7天)(返回数值) 74 * 75 * @param date 76 * @return 77 */ 78 public static Integer getWeekdayBeforeDateNum(Date date) { 79 80 java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd"); 81 82 long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 7; 83 84 date.setTime(beforeTime * 1000); 85 86 return Integer.parseInt(formatter.format(date)); 87 88 } 89 90 /** 91 * 获取一月前的日期(当前日期往前推30天) 92 * 93 * @param date 94 * @return 95 */ 96 public static String getMonthBeforeDate(Date date) { 97 98 java.text.Format formatter = new java.text.SimpleDateFormat( 99 "yyyy-MM-dd"); 100 101 long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30; 102 103 date.setTime(beforeTime * 1000); 104 105 return formatter.format(date); 106 107 } 108 109 /** 110 * 获取一月前的日期(当前日期往前推30天)(返回数值) 111 * 112 * @param date 113 * @return 114 */ 115 public static Integer getMonthBeforeDateNum(Date date) { 116 117 java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd"); 118 119 long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30; 120 121 date.setTime(beforeTime * 1000); 122 123 return Integer.parseInt(formatter.format(date)); 124 125 } 126 127 /** 128 * 获取三月前的日期(当前日期往前推90天) 129 * 130 * @param date 131 * @return 132 */ 133 public static String get3MonthBeforeDate(Date date) { 134 135 java.text.Format formatter = new java.text.SimpleDateFormat( 136 "yyyy-MM-dd"); 137 138 long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30 * 3; 139 140 date.setTime(beforeTime * 1000); 141 142 String d = formatter.format(date); 143 return d; 144 } 145 146 /** 147 * 获取三月前的日期(当前日期往前推30天)(返回数值) 148 * 149 * @param date 150 * @return 151 */ 152 public static Integer get3MonthBeforeDateNum(Date date) { 153 154 java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd"); 155 156 long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30 * 3; 157 158 date.setTime(beforeTime * 1000); 159 160 return Integer.parseInt(formatter.format(date)); 161 162 } 163 164 /** 165 * 获取一年后的日期 166 * 167 * @return 168 */ 169 public static String getNextYear(int chooseYear, Date date) { 170 171 java.text.Format formatter = new java.text.SimpleDateFormat( 172 "yyyy-MM-dd"); 173 174 long beforeTime = (date.getTime() / 1000) + 60 * 60 * 24 * 365 175 * chooseYear; 176 177 date.setTime(beforeTime * 1000); 178 179 return formatter.format(date); 180 } 181 182 /** 183 * 日期转换成字符串 184 * 185 * @param date 186 * @return 187 */ 188 public static String convertDateToStr(Date date) { 189 java.text.Format formatter = new java.text.SimpleDateFormat( 190 "yyyy-MM-dd"); 191 return formatter.format(date); 192 193 } 194 195 /** 196 * 日期转换成字符串 yyyy-MM-dd HHmmss 197 * 198 * @param date 199 * @return 200 */ 201 public static String convertTimeToStr(Date date) { 202 java.text.Format formatter = new java.text.SimpleDateFormat( 203 "yyyy-MM-dd HH:mm:ss"); 204 return formatter.format(date); 205 206 } 207 208 /** 209 * 将日期转为数值 210 * 211 * @param date 212 * @return 213 */ 214 public static Integer convertDateToNum(Date date) { 215 java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd"); 216 return Integer.parseInt(formatter.format(date)); 217 } 218 /** 219 * 将字符串日期转为数值 220 * 221 * @param date 222 * @return 223 */ 224 public static Integer convertStrToNum(String date) { 225 226 if (date.contains("-")) { 227 date = date.replace("-", ""); 228 } else if (date.contains(".")) { 229 date = date.replace(".", ""); 230 } else if (date.contains("/")) { 231 date = date.replace("/", ""); 232 } 233 234 return Integer.parseInt(date); 235 } 236 237 /** 238 * 时间转换器 第一个参数 要转化的数据类型 --- java.util.Date 第二个参数 要转化的数据 --- "2010-12-12" 239 * 240 */ 241 public static Date convertStrTODate( String str,Class<Date> type,String datePattern) { 242 243 if (str == null) { 244 return null; 245 } else { 246 if (type == java.util.Date.class) { 247 if (str instanceof String) { 248 try { 249 SimpleDateFormat sdf = new SimpleDateFormat(datePattern); 250 return sdf.parse(str); 251 } catch (ParseException e) { 252 throw new RuntimeException("您输入的数据格式不对"); 253 } 254 } else { 255 throw new RuntimeException("您要转化的数据输入不是String类型"); 256 } 257 } else { 258 throw new RuntimeException("您要转化的数据类型不对"); 259 } 260 } 261 } 262 263 /** 264 * 根据生日计算年龄 265 * 266 * @param birthDay 267 * @return 268 * @throws Exception 269 */ 270 public static int getAge(Date birthDay) throws Exception { 271 Calendar cal = Calendar.getInstance(); 272 273 if (cal.before(birthDay)) { 274 throw new IllegalArgumentException( 275 "The birthDay is before Now.It's unbelievable!"); 276 } 277 int yearNow = cal.get(Calendar.YEAR); 278 int monthNow = cal.get(Calendar.MONTH); 279 int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); 280 cal.setTime(birthDay); 281 282 int yearBirth = cal.get(Calendar.YEAR); 283 int monthBirth = cal.get(Calendar.MONTH); 284 int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); 285 286 int age = yearNow - yearBirth; 287 288 if (monthNow <= monthBirth) { 289 if (monthNow == monthBirth) { 290 // monthNow==monthBirth 291 if (dayOfMonthNow < dayOfMonthBirth) { 292 age--; 293 } else { 294 // do nothing 295 } 296 } else { 297 // monthNow>monthBirth 298 age--; 299 } 300 } else { 301 // monthNow<monthBirth 302 // donothing 303 } 304 305 return age; 306 } 307 ///////////////////////////////////////根据时间段获取时间的集合////////////////////////////////////////////////////////////////// 308 public static List<String> getDateListBydates(String s1,String s2,String format) throws ParseException{ 309 List<String>list=new ArrayList<String>(); 310 //String s1 = "2012-02-01"; 311 //String s2 = "2012-04-04"; 312 SimpleDateFormat sdf = new SimpleDateFormat(format); 313 Date begin=sdf.parse(s1); 314 Date end=sdf.parse(s2); 315 double between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒 316 double day=between/(24*3600); 317 for(int i = 0;i<=day;i++){ 318 Calendar cd = Calendar.getInstance(); 319 cd.setTime(sdf.parse(s1)); 320 cd.add(Calendar.DATE, i);//增加一天 321 //cd.add(Calendar.MONTH, n);//增加一个月 322 log.info(sdf.format(cd.getTime())); 323 list.add(sdf.format(cd.getTime())); 324 } 325 return list; 326 } 327 //获取指定年月的总天数 328 public static int getLastDay(int year, int month) { 329 int day = 1; 330 Calendar cal = Calendar.getInstance(); 331 cal.set(year,month - 1,day); 332 int last = cal.getActualMaximum(Calendar.DATE); 333 System.out.println(last); 334 return last; 335 } 336 //获取指定年月的日期 337 @SuppressWarnings("unchecked") 338 public static List<String> getDatesByMonth(int year, int month){ 339 List<String> list=new ArrayList(); 340 String yyyy=year+""; 341 String mm=month+""; 342 String dd="01"; 343 if(month<10){ 344 mm="0"+month; 345 } 346 int num=getLastDay(year, month); 347 for(int i=1;i<=num;i++){ 348 if(i<10){ 349 dd="0"+i; 350 }else{ 351 dd=i+""; 352 } 353 list.add(yyyy+"-"+mm+"-"+dd); 354 System.out.println(yyyy+"-"+mm+"-"+dd); 355 } 356 return list; 357 } 358 /** 359 * 360 * @param datestr 解析字符串 如2014-04-1716:38:57 361 * @param sourceDateformat 源日期格式 如yyyy-MM-ddHH:mm:ss 362 * @param formatStr 要转换的日期格式 如(yyyy-MM-dd HH:mm:ss) 363 * @return 364 */ 365 public static String strTOdateTOStr(String datestr,String sourceDateformat,String targetDateformat){ 366 // String str="2014-04-1716:38:57"; 367 try { 368 SimpleDateFormat sourceFormat = new SimpleDateFormat(sourceDateformat); 369 SimpleDateFormat targetFormat = new SimpleDateFormat(targetDateformat); 370 return targetFormat.format(sourceFormat.parse(datestr)); 371 } catch (Exception e) { 372 log.info("strTOdateTOStr:"+e); 373 } 374 return null; 375 376 } 377 ///////////////////////////////////////////////////////////////////////////////////////////////////////// 378 379 public static void main(String[] args) throws Exception{ 380 //getDatesByMonth(2013,2); 381 getDateListBydates("20130517","20130519","yyyyMMdd"); 382 383 } 384 385 }