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
| package com.shebao.dispatch.common.utils;
import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.DateUtil;
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*;
public class DateUtils extends org.apache.commons.lang3.time.DateUtils{
public static final String TL_DF_DATE = "yyyy-MM-dd"; public static final String TL_DF_DATETIME = "yyyy-MM-dd HH:mm:ss"; public static final String TL_DF_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; public static final String TL_DF_YYYY_DATETIME = "yyyyMMddHHmmss";
private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM.dd HH", "yyyy.MM", "yyyy年MM月dd日", "yyyy年MM月dd日 HH时mm分ss秒", "yyyy年MM月dd日 HH时mm分", "yyyy年MM月dd日 HH时", "yyyy年MM月", "yyyy"};
private static Set<String> DATEFORMAT_SET = new LinkedHashSet<>(); static { DATEFORMAT_SET.add(TL_DF_DATE); DATEFORMAT_SET.add(TL_DF_DATETIME); DATEFORMAT_SET.add("yyyy/MM/dd"); DATEFORMAT_SET.add("yyyy/MM/dd HH:mm:ss"); DATEFORMAT_SET.add("yyyyMMdd"); DATEFORMAT_SET.add("yyyyMMdd HH:mm:ss"); DATEFORMAT_SET.add(TL_DF_YYYY_DATETIME); DATEFORMAT_SET.add(TL_DF_UTC); }
public static Date parseDateCommon(Object str) { if (str == null) { return null; } if ((str.toString().matches("^[0-9]*$")) && str.toString().length() > 4) { return new Date(Long.parseLong(str.toString())); } try { return parseDate(str.toString(), parsePatterns); } catch (ParseException e) { return null; } }
public static int currentTimeInSeconds() { return (int) (System.currentTimeMillis()/1000); } public static String formateToDateTimeNo(Date date){ return formatNullable(date, TL_DF_YYYY_DATETIME); } public static String formatTo(Date date, String fmt) { return formatNullable(date, fmt); } public static String formatToDate(Date date) { return formatNullable(date, TL_DF_DATE); } public static String formatToDatetime(Date date) { return formatNullable(date, TL_DF_DATETIME); }
public static String formatToYmdDate(Date date) { return formatNullable(date, TL_DF_DATE); }
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static String currentAsDatetime() { return DATE_TIME_FORMATTER.format(LocalDateTime.now()); }
public static Date fromSeconds(Integer timestampInSeconds) { if(timestampInSeconds == null || timestampInSeconds < 0) { return new Date(Long.MAX_VALUE); } return new Date(timestampInSeconds.longValue() * 1000); }
static Date getDateFromUtcString(String str) { try { return getDateFormat(TL_DF_UTC).parse(str); } catch (ParseException e) { return null; } }
public static Long getDayStartTime(String params) { if (StringUtils.isEmpty(params)) { return null; } Date date = parseDate(params); if (date != null) { LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.of("+8")).getEpochSecond(); } return null; }
public static Long getDayEndTime(String params) { if (StringUtils.isEmpty(params)) { return null; } Date date = parseDate(params); if (date != null) { LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); return LocalDateTime.of(localDate, LocalTime.MAX).toInstant(ZoneOffset.of("+8")).getEpochSecond(); } return null; }
public static Date getCurrentMonthFirstDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.DAY_OF_MONTH,1); return calendar.getTime(); }
public static Date getPreviousMonthLastDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setTime(date); calendar.add(Calendar.MONTH,-1); calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); return calendar.getTime(); }
public static Date getCurrentMonthLastDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); return calendar.getTime(); }
public static int getTimeInSeconds(Date date) { if (date == null) { return 0; } return (int) (date.getTime() / 1000); }
public static Date parseDate(String date) { try { return getDateFormat(TL_DF_DATE).parse(date); } catch (ParseException e) { return null; } }
public static Date parseDate(String date, String fmt) { try { return getDateFormat(fmt).parse(date); } catch (ParseException e) { return null; } }
public static Date parseDatetime(String date) { try { return getDateFormat(TL_DF_DATETIME).parse(date); } catch (ParseException e) { return null; } }
public static Date tryParseDate(String str) { if(StringUtils.isBlank(str)) { return null; } Date date; for (String fmt : DATEFORMAT_SET) { date = parseDate(str, fmt); if(date != null) { return date; } } try { double num = Double.parseDouble(str); if(DateUtil.isValidExcelDate(Double.valueOf(str))) { return DateUtil.getJavaDate(num); } } catch (NumberFormatException ignored) { } return null; }
private static String formatNullable(Date date, String fmt) { DateFormat formatter = getDateFormat(fmt); if(date == null || date.getTime() == 0) { return ""; } return formatter.format(date); }
private static DateFormat getDateFormat(String fmt) { return new SimpleDateFormat(fmt); }
public static int calcMonthDifference(Date sourceStart, Date sourceEnd) { Calendar start = Calendar.getInstance(); Calendar end = Calendar.getInstance(); start.setTime(sourceStart); end.setTime(sourceEnd); int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH); int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12; return month + result; }
public static long getDayDifference(Date sourceStart, Date sourceEnd) { long dif = sourceEnd.getTime() - sourceStart.getTime(); long day= dif /(24*60*60*1000); return day; }
public static Date getDateAddDayDifference(Date source, int day) { Calendar c = Calendar.getInstance(); c.setTime(source); c.add(Calendar.DAY_OF_MONTH, day); Date dif = c.getTime(); return dif; }
private static final Object lockObj = new Object();
private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>(); public static final String YYYY_MM = "yyyy-MM"; public static final String YYYY_MM_CHINESE = "yyyy年MM月";
private static SimpleDateFormat getSimpleDateFormat(final String pattern) { ThreadLocal<SimpleDateFormat> tl = sdfMap.get(pattern); if (tl == null) { synchronized (lockObj) { tl = sdfMap.get(pattern); if (tl == null) {
tl = new ThreadLocal<SimpleDateFormat>() {
@Override protected SimpleDateFormat initialValue() { System.out.println("thread: " + Thread.currentThread() + " init pattern: " + pattern); return new SimpleDateFormat(pattern); } }; sdfMap.put(pattern, tl); } } } return tl.get(); }
public static String getDateStr(String pattern) { String date = getSimpleDateFormat(pattern).format(new Date(System.currentTimeMillis())); return date; }
public static Calendar getTime() { Calendar time=Calendar.getInstance(); return time; }
public static Calendar getTime(long timeMillis) { Calendar time=getTime(); time.setTimeInMillis(timeMillis); return time; } public static Date getDate() { return getTime().getTime(); } public static Date getDate(long timeMillis) { return getTime(timeMillis).getTime(); }
public static String getYYYYMMDD() { return getSimpleDateFormat("yyyyMMdd").format(getDate()); } public static String getYYYYMMDD(long time) { return getSimpleDateFormat("yyyyMMdd").format(getDate(time)); } public static String getYYYYMMDD(Date date) { return null==date ? "" : getSimpleDateFormat(TL_DF_DATE).format(date); } public static String getYYYYMMDDNullRetrunNoTimeLimit(Date date) { return null==date ? "无期限" : getSimpleDateFormat(TL_DF_DATE).format(date); } public static String getHHmmss() { return getSimpleDateFormat("HHmmss").format(getDate()); } public static String getYYYYMMDDHHmmss(Date date) { return getSimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); } public static String getYYYYMM() { return getSimpleDateFormat("yyyyMM").format(getDate()); } public static String getYYYYMM(Date date) { if(date == null) { return null; } SimpleDateFormat YYYY_MM_FORMAT = new SimpleDateFormat(YYYY_MM); return YYYY_MM_FORMAT.format(date); } public static String getYYYYMMWithChinese(Date date) { if(date == null) { return ""; } SimpleDateFormat YYYY_MM_FORMAT = new SimpleDateFormat(YYYY_MM_CHINESE); return YYYY_MM_FORMAT.format(date); } public static String getYYYYMM(long timeMillis) { return getSimpleDateFormat("yyyyMM").format(getDate(timeMillis)); } public static String getMMDD() { return getSimpleDateFormat("MMdd").format(getTime()); } public static String getMMDD(long timeMillis) { return getSimpleDateFormat("MMdd").format(getDate(timeMillis)); } public static String getDD(long timeMillis) { return getSimpleDateFormat("dd").format(getDate(timeMillis)); }
}
|