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 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
|
public class DateUtils { public enum DateType { NORM_DATE_PATTERN("yyyy-MM-dd"), NORM_DATETIME_PATTERN("yyyy-MM-dd HH:mm:ss"), NORM_TIME_PATTERN("HH:mm:ss"), NORM_DATETIME_MINUTE_PATTERN("yyyy-MM-dd HH:mm"), NORM_DATETIME_MS_PATTERN("yyyy-MM-dd HH:mm:ss.SSS"), CHINESE_DATE_PATTERN("yyyy年MM月dd日"), PURE_DATE_PATTERN("yyyyMMdd"), PURE_TIME_PATTERN("HHmmss"), PURE_DATETIME_PATTERN("yyyyMMddHHmmss"), PURE_DATETIME_MS_PATTERN("yyyyMMddHHmmssSSS"), PURE_DATE_MD_PATTERN("MMdd"), ; private String format;
DateType(String format) { this.format = format; }
public String getFormat() { return format; } }
private static final String[] DAY_OF_WEEK = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
public static String stampToDate(long time, String format) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format); return dateTimeFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault())); }
public static String dateToString(Date date, DateType type) { ZonedDateTime now = dateToZonedDateTime(date); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(type.getFormat()); return now.format(formatter); }
public static String dateToString(Date date) { return dateToString(date, DateType.NORM_DATETIME_MINUTE_PATTERN); }
public static String dateToString(DateType type) { return dateToString(new Date(), type); }
public static String dateToString() { return dateToString(new Date(), DateType.NORM_DATETIME_MINUTE_PATTERN); }
public static Date strToDate(String dateStr, DateType dateType) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateType.getFormat()); return Date.from(LocalDate.parse(dateStr, formatter).atStartOfDay(ZoneId.systemDefault()).toInstant());
}
public static Date strToDateTime(String dateStr, DateType dateType) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateType.getFormat()); return Date.from(LocalDateTime.parse(dateStr, formatter).atZone(ZoneId.systemDefault()).toInstant()); }
public static boolean isToday(Date date) { return LocalDate.now().equals(dateToLocalDate(date)); }
public static Date getBeforeOrAfterNumDay(Date date, long num) { return localDateToDate(dateToLocalDate(date).plusDays(num)); }
public static String getBeforeOrAfterNumMonth(int num) { String date = LocalDate.now().plusMonths(num).toString(); return date.substring(0, date.lastIndexOf("-")); }
public static Date addYear(Date date, int num) { return localDateTimeToDate(dateToLocalDateTime(date).plusYears(num)); }
public static String getCurrentYearAndMonth(String separator) { String month = LocalDate.now().getMonth().getValue() >= 10 ? "" + LocalDate.now().getMonth().getValue() : "0" + LocalDate.now().getMonth().getValue(); return LocalDate.now().getYear() + separator + month; }
public static String getCurrentYearWithFirstMonth(String separator) { return LocalDate.now().getYear() + separator + "01"; }
public static boolean checkDate(String beginDate, String endDate, String split) { return Integer.parseInt(endDate.replace(split, "")) - Integer.parseInt(beginDate.replace(split, "")) >= 0; }
public static String getCurrentMonthAndDay(String separator) { String month = LocalDate.now().getMonth().getValue() >= 10 ? "" + LocalDate.now().getMonth().getValue() : "0" + LocalDate.now().getMonth().getValue(); String day = LocalDate.now().getDayOfMonth() >= 10 ? "" + LocalDate.now().getDayOfMonth() : "0" + LocalDate.now().getDayOfMonth(); return month + separator + day; }
public static Date addMonth(Date date, int num) { return localDateTimeToDate(dateToLocalDateTime(date).plusMonths(num)); }
public static Date addDay(Date date, int num) { return localDateTimeToDate(dateToLocalDateTime(date).plusDays(num)); }
public static Date addHours(Date date, int num) { return localDateTimeToDate(dateToLocalDateTime(date).plusHours(num)); }
public static Date addMinutes(Date date, int num) { return localDateTimeToDate(dateToLocalDateTime(date).plusMinutes(num)); }
public static Date addSeconds(Date date, int num) { return localDateTimeToDate(dateToLocalDateTime(date).plusSeconds(num)); }
public static Date addNanos(Date date, int num) { return localDateTimeToDate(dateToLocalDateTime(date).plusNanos(num)); }
public static Date addWeeks(Date date, int num) { return localDateTimeToDate(dateToLocalDateTime(date).plusWeeks(num)); }
public static int getDayOfWeek() { return LocalDate.now().atStartOfDay(ZoneId.systemDefault()).getDayOfWeek().getValue(); }
public static String getDayOfWeekWithChinese() { return DAY_OF_WEEK[getDayOfWeek() - 1]; }
public static int getDayOfWeek(Date date) { return dateToLocalDate(date).atStartOfDay(ZoneId.systemDefault()).getDayOfWeek().getValue(); }
public static String getDayOfWeekWithChinese(Date date) { return DAY_OF_WEEK[getDayOfWeek(date) - 1]; }
public static boolean belongDateRange(Date date, Date beginTime, Date endTime) { return date.before(beginTime) && date.after(endTime); }
public static int getNowHour() { return LocalDateTime.now().getHour(); }
public static int getAgeByBirthday(Date birthday) { LocalDateTime birthdayDate = dateToLocalDateTime(birthday); if (LocalDateTime.now().isBefore(birthdayDate)) { return 0; } Duration duration = Duration.between(birthdayDate, LocalDateTime.now()); return (int) duration.toDays(); }
public static int getWeekOfYear(Date date) { WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1); return dateToLocalDateTime(date).get(weekFields.weekOfYear()); }
public static String[] getPreviousBeginHourAndEndHour(Date date) { String[] lastHours = new String[3]; String hour = dateToLocalDateTime(date).getHour() - 1 + ""; if (hour.length() < 2) { hour = 0 + hour; } LocalDate localDateTime = dateToLocalDate(date); lastHours[0] = localDateTime + " " + hour + ":00:00"; lastHours[1] = localDateTime + " " + hour + ":59:59"; lastHours[2] = hour; return lastHours; }
public static String[] getPreviousBeginDayAndEndDay() { return getPreviousBeginDayAndEndDay(new Date()); }
public static String[] getPreviousBeginDayAndEndDay(Date date) { LocalDate prevDay = dateToLocalDate(date).plusWeeks(-1); DayOfWeek week = prevDay.getDayOfWeek(); int value = week.getValue(); return new String[]{prevDay.minusDays(value - 1).toString(), prevDay.plusDays(7 - value).toString()}; }
public static List<String> getDatesBetweenStartDateAndEndDate(String start, String end) { List<String> list = new ArrayList<>(10); LocalDate startDate = LocalDate.parse(start); LocalDate endDate = LocalDate.parse(end); long distance = ChronoUnit.DAYS.between(startDate, endDate); if (distance < 1) { return list; } Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString())); return list; }
public static List<Date> getDatesBetweenStartDateAndEndDate(Date start, Date end) { List<Date> list = new ArrayList<>(10); LocalDate startDate = dateToLocalDate(start); LocalDate endDate = dateToLocalDate(end); long distance = ChronoUnit.DAYS.between(startDate, endDate); if (distance < 1) { return list; } Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(localDateToDate(f))); return list; }
public static String getLastDayOfMonth(String yearMonth) { LocalDate resDate = LocalDate.parse(yearMonth + "-01"); Month month = resDate.getMonth(); int length = month.length(resDate.isLeapYear()); return LocalDate.of(resDate.getYear(), month, length).toString();
}
public static String getTimeBySeconds(int seconds) { if (seconds > 60 * 60 * 24 - 1) { return "23:59:59"; } seconds = seconds % 86400; String hours = String.valueOf(seconds / 3600).length() > 1 ? String.valueOf(seconds / 3600) : "0" + (seconds / 3600); seconds = seconds % 3600; String minutes = String.valueOf(seconds / 60).length() > 1 ? String.valueOf(seconds / 60) : "0" + (seconds / 60); String second = String.valueOf(seconds % 60).length() > 1 ? String.valueOf(seconds % 60) : "0" + (seconds % 60); return hours + ":" + minutes + ":" + second; }
public static int getTimeDifference(Date begin, Date end) { return (int) (((end.getTime() - begin.getTime()) / (1000))); }
public static List<String> getRangeOfDates(String beginTime, String endTime) { List<String> dayList = new ArrayList<>(); LocalDate startDate = LocalDate.parse(beginTime); LocalDate endDate = LocalDate.parse(endTime); long days = ChronoUnit.DAYS.between(startDate, endDate); LongStream.range(0, days + 1).forEach(d -> dayList.add(startDate.plusDays(d).toString())); return dayList; }
public static ZonedDateTime dateToZonedDateTime(Date date) { return ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); }
public static Date zonedDateTimeToDate(ZonedDateTime zonedDateTime) { return Date.from(zonedDateTime.toInstant()); }
public static LocalDate dateToLocalDate(Date date) { return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); }
public static Date localDateToDate(LocalDate localDate) { return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); }
public static LocalDateTime dateToLocalDateTime(Date date) { return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); }
public static Date localDateTimeToDate(LocalDateTime localDateTime) { return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); }
}
|