1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
| public class DateUtil { public static final String YYYY_MM_DD = "yyyy-MM-dd"; public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static final String YYYY_MMDD = "yyyyMMdd"; public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; public static final String YYYY = "yyyy"; public static final String YYYY_MM = "yyyyMM";
private final static SimpleDateFormat dateformater = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS); private final static SimpleDateFormat dateformater1 = new SimpleDateFormat(YYYY_MM_DD); private final static SimpleDateFormat dateformater2 = new SimpleDateFormat(YYYY_MMDD); private final static SimpleDateFormat dateformater3 = new SimpleDateFormat(YYYYMMDDHHMMSS); private final static SimpleDateFormat dateformater4 = new SimpleDateFormat(YYYY_MM); private final static SimpleDateFormat dateformater5 = new SimpleDateFormat(YYYY); public static boolean defaultTimeFlag = true; public final static String startTimePoint = " 00:00:00"; public final static String endTimePoint = " 23:59:59";
public static Date getNowDate() { Date date = null; try { date = parseUtilDate(dateformater.format(new Date())); } catch (Exception e) { e.printStackTrace(); } return date; }
public static java.lang.String formatSecondDate(Date d) throws ParseException { return dateformater3.format(d); } public static String getFormatNowDay() { return dateformater1.format(new Date()); }
public static String getSNowDay() { return dateformater.format(new Date()); } public static String getFormatDay(Date d) { return dateformater2.format(d); } public static String getMonthFirstDay() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, calendar .getActualMinimum(Calendar.DAY_OF_MONTH)); return dateformater1.format(calendar.getTime()); } public static String getMonthLastDay() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, calendar .getActualMaximum(Calendar.DAY_OF_MONTH)); return dateformater1.format(calendar.getTime()); }
private final static long DAY_IN_MILLISECOND = 1000 * 3600 * 24;
public final static long DAY_IN_SECOND = 3600 * 24;
private DateUtil() { }
public static java.util.Date getDate(java.util.Date startDate, int days) { java.util.Date returnDay = dateDayAdd(startDate, days); return returnDay; }
public static java.util.Date getMonthDate(java.util.Date startDate, int months) { java.util.Date returnDay = dateMonthAdd(startDate, months); return returnDay; }
public static String getCurMonth() { return new SimpleDateFormat("yyyy-MM").format(new java.util.Date()); }
public static java.util.Date getMonthBegin(java.util.Date month) { GregorianCalendar ca = new GregorianCalendar(); ca.setTime(month); int year = ca.get(GregorianCalendar.YEAR); int mon = ca.get(GregorianCalendar.MONTH); GregorianCalendar gCal = new GregorianCalendar(year, mon, 1); return gCal.getTime(); }
public static java.util.Date getMonthBegin(int year, int month) { if (month <= 0 || month > 12) throw new IllegalArgumentException("month must from 1 to 12"); GregorianCalendar gCal = new GregorianCalendar(year, month - 1, 1); return gCal.getTime(); } public static java.util.Date getMonthEnd(java.util.Date month) { GregorianCalendar ca = new GregorianCalendar(); ca.setTime(month); int year = ca.get(GregorianCalendar.YEAR); int mon = ca.get(GregorianCalendar.MONTH); int lastDay = ca.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); GregorianCalendar gCal = new GregorianCalendar(year, mon, lastDay); return gCal.getTime(); }
public static java.util.Date getMonthEnd(int year, int month) { java.util.Date start = getMonthBegin(year, month); return getMonthEnd(start); }
public static java.util.Date getWeekBegin(java.util.Date date) { GregorianCalendar gCal = new GregorianCalendar(); gCal.setTime(date); int startDay = gCal.getActualMinimum(GregorianCalendar.DAY_OF_WEEK); gCal.set(GregorianCalendar.DAY_OF_WEEK, startDay); return gCal.getTime(); }
public static java.util.Date getWeekEnd(java.util.Date date) { GregorianCalendar gCal = new GregorianCalendar(); gCal.setTime(date); int lastDay = gCal.getActualMaximum(GregorianCalendar.DAY_OF_WEEK); gCal.set(GregorianCalendar.DAY_OF_WEEK, lastDay); return gCal.getTime(); }
public static int getDays(java.sql.Timestamp beginTime, java.sql.Timestamp endTime) { int days = (int) ((endTime.getTime() - beginTime.getTime()) / DAY_IN_MILLISECOND); return days; }
public static java.sql.Date parseSqlDate(String strDate) { return java.sql.Date.valueOf(strDate); }
public static java.util.Date parseUtilDate(String strDate) throws ParseException { return dateformater.parse(strDate); }
public static java.util.Date parseDayDate(String strDate) throws ParseException { return dateformater1.parse(strDate); }
public static java.util.Date parseDay2Date(String strDate) throws ParseException { return dateformater2.parse(strDate); }
public static java.util.Date parse2Date(String strDate) throws ParseException { java.util.Date d = null; try { d =dateformater2.parse(strDate); } catch (Exception e) { try { d = dateformater4.parse(strDate); } catch (Exception e1) { d = dateformater5.parse(strDate); } } return d; }
public static java.lang.String format2Date(Date date) throws ParseException { java.lang.String d = null; try { d =dateformater2.format(date); } catch (Exception e) { try { d = dateformater4.format(date); } catch (Exception e1) { d = dateformater5.format(date); } } return d; }
public static java.lang.String formatDayDate(Date d) throws ParseException { return dateformater1.format(d); }
public static java.lang.String formatDate(Date d) throws ParseException { return dateformater2.format(d); }
public static java.util.Date parseLastMonthDate() { GregorianCalendar gCal = new GregorianCalendar(); gCal.setTime(new Date()); gCal.add(GregorianCalendar.MONTH, -1); return gCal.getTime(); } public static java.sql.Timestamp parseLastMonthTimestamp() { GregorianCalendar gCal = new GregorianCalendar(); gCal.setTime(new Date()); gCal.add(GregorianCalendar.MONTH, -1); return new java.sql.Timestamp(gCal.getTime().getTime()); }
public static java.sql.Timestamp getNextTimestamp(Timestamp e,int i) { if(e==null) return new java.sql.Timestamp(new Date().getTime()); GregorianCalendar gCal = new GregorianCalendar(); gCal.setTime(new Date(e.getTime()+i*1000*60*60*24)); return new java.sql.Timestamp(gCal.getTime().getTime()); }
private static java.util.Date dateDayAdd(java.util.Date date, int days) { long now = date.getTime() + days * DAY_IN_MILLISECOND; return (new java.util.Date(now)); }
@SuppressWarnings("unused") private static java.sql.Timestamp dateDateTimeAdd( java.sql.Timestamp timestamp, int minutes) { long now = timestamp.getTime() + minutes * 1000 * 60; return (new java.sql.Timestamp(now)); } public static long getDiffMinu(Timestamp startDate, Timestamp endDate) { long startTime = startDate.getTime(); long endTime = endDate.getTime(); System.out.println(startTime); System.out.println(endTime);
long minu = (long) ((endTime - startTime) / (1000 * 60)); return minu; }
private static java.util.Date dateMonthAdd(java.util.Date date, int months) { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); int m = cal.get(GregorianCalendar.MONTH) + months; if (m < 0) m += -12; cal.roll(GregorianCalendar.YEAR, m / 12); cal.roll(GregorianCalendar.MONTH, months); return cal.getTime(); }
@SuppressWarnings("unused") private static String getDateTimeStr(String str) { if (str.indexOf(" ") == -1) str += " 00:00:00"; else if (str.lastIndexOf(":") == 13) str += ":00"; return str; }
public static int GetDaysOfMonth(int iYear, int Month) { int days = 0; switch (Month) { case 1: days = 31; break; case 2: if (IsRuYear(iYear)) { days = 29; } else { days = 28; }
break; case 3: days = 31; break; case 4: days = 30; break; case 5: days = 31; break; case 6: days = 30; break; case 7: days = 31; break; case 8: days = 31; break; case 9: days = 30; break; case 10: days = 31; break; case 11: days = 30; break; case 12: days = 31; break; }
return days;
}
private static boolean IsRuYear(int year) {
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { return true; } else { return false; } }
public static String getWeekOfDate(Date dt) { String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"}; Calendar cal = Calendar.getInstance();
cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return weekDays[w];
}
public static String convertDateFormat(Date date, String format) { String time = ""; try { time = (new SimpleDateFormat(format)).format(date); } catch (RuntimeException e) { } return time; }
public static Date getDateFromSeconds(long seconds) { Calendar ca = Calendar.getInstance(); ca.setTimeInMillis(seconds * 1000); return ca.getTime(); }
public static long getSecondsFromDurTime(String duration) { long seconds = 0; String[] dur = duration.split(":"); seconds = new Long(dur[0]).longValue() * 3600 + new Long(dur[1]).longValue() * 60 + new Long(dur[2]).longValue(); return seconds; }
public static String formatDurTime(long durLong, String formatStr) { String durTime = ""; if (formatStr == null || "".equals(formatStr)) formatStr = "hh:mm:ss"; String[] fmtArray = formatStr.split(":"); for (int i = 0; i < fmtArray.length; i++) { if (fmtArray[i].equals("hh") || fmtArray[i].endsWith("小时")) { long hl = durLong / 3600; if (hl == 0) continue; durLong = durLong % 3600; if (fmtArray[i].endsWith("小时")) durTime += hl + fmtArray[i]; else durTime += hl + ":"; } else if (fmtArray[i].equals("mm") || fmtArray[i].startsWith("分")) { long ml = durLong / 60; durLong = durLong % 60; if (fmtArray[i].startsWith("分")) durTime += ml + fmtArray[i]; else durTime += ml + ":"; } else if (fmtArray[i].equals("ss") || fmtArray[i].equals("秒")) { long sl = durLong; if (fmtArray[i].equals("秒")) durTime += sl + "秒"; else durTime += sl; } }
return durTime; }
public static Date getFinallyDate(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String fm = format.format(date); fm += " 23:59:59"; try { return DateUtils.parseDate(fm, new String[] { "yyyyMMdd HH:mm:ss" }); } catch (ParseException e1) { return null; } }
public static Date getStartDate(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String fm = format.format(date); fm += " 00:00:00"; try { return DateUtils.parseDate(fm, new String[] { "yyyyMMdd HH:mm:ss" }); } catch (ParseException e1) { return null; } }
public static void main(String[] args) { System.out.println(DateUtil.getDate(getNowDate(),3)); }
public static Date dateStringToDate(String date, String pattern){ DateFormat df = new SimpleDateFormat(pattern); Date today = null; try { today = df.parse(date); }catch (NullPointerException e){ today = null; } catch (ParseException e) { today = null; } return today; }
public static Date addDay(Date dDate, long iNbDay) { return addHour(dDate, iNbDay * 24); }
public static Date addHour(Date dDate, long hour) { return addSecond(dDate, hour * 60L * 60L); }
public static Date addSecond(Date dDate, long second) {
long datems = dDate.getTime(); long secondms = second * 1000L; long newDateMs = datems + secondms; Date result = new Date(newDateMs); return result;
}
public static String getDateStr(String rource,String pattern,String format){ if(rource != null && !"".equals(rource)){ SimpleDateFormat df = new SimpleDateFormat(pattern); Date date = null; try { date = df.parse(rource); } catch (ParseException e) { e.printStackTrace(); } if(date != null){ SimpleDateFormat df2 = new SimpleDateFormat(format); return df2.format(date); }else{ return ""; } }else{ return ""; } } }
|