Commit 6b8ee7094aee760f791a5bad1235ef4457d95809
1 parent
466654cbd6
Exists in
master
1
Showing 1 changed file with 788 additions and 0 deletions
parent/core.sdk/src/main/java/com/lyms/util/DateUtil.java
View file @
6b8ee70
| 1 | +package com.lyms.util; | |
| 2 | + | |
| 3 | +import java.text.ParseException; | |
| 4 | +import java.text.SimpleDateFormat; | |
| 5 | +import java.util.Calendar; | |
| 6 | +import java.util.Date; | |
| 7 | +import java.util.GregorianCalendar; | |
| 8 | +import java.util.concurrent.locks.Lock; | |
| 9 | +import java.util.concurrent.locks.ReentrantLock; | |
| 10 | + | |
| 11 | +import org.joda.time.DateTime; | |
| 12 | + | |
| 13 | +public class DateUtil { | |
| 14 | + private static Lock lock = new ReentrantLock(); | |
| 15 | + public static SimpleDateFormat dd = new SimpleDateFormat("dd"); | |
| 16 | + public static SimpleDateFormat ymd = new SimpleDateFormat("yyyyMMdd"); | |
| 17 | + public static SimpleDateFormat y_m_d = new SimpleDateFormat("yyyy-MM-dd"); | |
| 18 | + public static SimpleDateFormat md = new SimpleDateFormat("MM-dd"); | |
| 19 | + public static SimpleDateFormat m_d = new SimpleDateFormat("MM/dd"); | |
| 20 | + public static SimpleDateFormat y_m_d_h_m_s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
| 21 | + public static SimpleDateFormat y_m_d_h_m1 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); | |
| 22 | + | |
| 23 | + public static SimpleDateFormat y_m_d_h_m = new SimpleDateFormat("yyyyMMddHHmm"); | |
| 24 | + public static SimpleDateFormat yyyyMMddHHmmssSSS = new SimpleDateFormat("yyyyMMddHHmmssSSS"); | |
| 25 | + public static final int DAY_SECONDS = 86399; | |
| 26 | + public static final int DAY_FULL_SECONDS = 86400; | |
| 27 | + | |
| 28 | + public static String getymd() { | |
| 29 | + return y_m_d.format(new Date()); | |
| 30 | + } | |
| 31 | + | |
| 32 | + public static String getymd6() { | |
| 33 | + return ymd.format(new Date()); | |
| 34 | + } | |
| 35 | + | |
| 36 | + public static String getymdhmss() { | |
| 37 | + return yyyyMMddHHmmssSSS.format(new Date()); | |
| 38 | + } | |
| 39 | + | |
| 40 | + public static Integer getDays(Date start, Date end) { | |
| 41 | + Integer days = 0; | |
| 42 | + if (start != null && end != null) { | |
| 43 | + start = getDayStartTime(start); | |
| 44 | + end = getDayStartTime(end); | |
| 45 | + Integer seconds = (int) ((end.getTime() - start.getTime()) / 1000); | |
| 46 | + days = seconds / DAY_FULL_SECONDS; | |
| 47 | + } | |
| 48 | + return days; | |
| 49 | + } | |
| 50 | + | |
| 51 | + public static Integer minute2Index(Date date) { | |
| 52 | + | |
| 53 | + DateTime dt = new DateTime(date.getTime()); | |
| 54 | + return dt.getMinuteOfDay() / 10; | |
| 55 | + } | |
| 56 | + | |
| 57 | + public static Integer getAge(Date birth) { | |
| 58 | + if (null == birth) { | |
| 59 | + return null; | |
| 60 | + } | |
| 61 | + java.util.Calendar calendar = Calendar.getInstance(); | |
| 62 | + Date date = new Date(); | |
| 63 | + calendar.setTime(date); | |
| 64 | + | |
| 65 | + Calendar c1 = Calendar.getInstance(); | |
| 66 | + Calendar c2 = Calendar.getInstance(); | |
| 67 | + c1.setTime(date); | |
| 68 | + c2.setTime(birth); | |
| 69 | + int year1 = c1.get(Calendar.YEAR); | |
| 70 | + int year2 = c2.get(Calendar.YEAR); | |
| 71 | + | |
| 72 | + int mouth1 = c1.get(Calendar.MONTH);//0当前 | |
| 73 | + int mouth2 = c2.get(Calendar.MONTH);//4 | |
| 74 | + int i = 0; | |
| 75 | + /* if(mouth2-mouth1>=0){ | |
| 76 | + int days1 = c1.get(Calendar.DAY_OF_MONTH);//当前10 | |
| 77 | + int days2 = c2.get(Calendar.DAY_OF_MONTH);//1 | |
| 78 | + if(days1<days2){ | |
| 79 | + i=i-1; | |
| 80 | + }else if(days1>days2){ | |
| 81 | + } | |
| 82 | + }else if(mouth1-mouth2<0){ | |
| 83 | + i=i-1; | |
| 84 | + }*/ | |
| 85 | + | |
| 86 | + if (mouth2 == mouth1) { | |
| 87 | + int days1 = c1.get(Calendar.DAY_OF_MONTH);//当前10 | |
| 88 | + int days2 = c2.get(Calendar.DAY_OF_MONTH);//1 | |
| 89 | + if (days1 < days2) { | |
| 90 | + i = i - 1; | |
| 91 | + } else if (days1 > days2) { | |
| 92 | + // i=i-1; | |
| 93 | + } | |
| 94 | + } else if (mouth2 > mouth1) { | |
| 95 | + i = i - 1; | |
| 96 | + } | |
| 97 | + | |
| 98 | + // if (birth == null) { | |
| 99 | + // return null; | |
| 100 | + // } | |
| 101 | + // Date now = new Date(); | |
| 102 | + // long m = now.getTime() - birth.getTime(); | |
| 103 | + // Long age = m / 31536000000L; | |
| 104 | + // return age.intValue(); | |
| 105 | + return (Math.abs(year1 - year2) + i); | |
| 106 | + } | |
| 107 | + | |
| 108 | + public static Integer getAge(Date birth, Date fmDate) { | |
| 109 | + if (birth == null || null == fmDate) { | |
| 110 | + return null; | |
| 111 | + } | |
| 112 | + long m = fmDate.getTime() - birth.getTime(); | |
| 113 | + Long age = m / 31536000000L; | |
| 114 | + return age.intValue(); | |
| 115 | + } | |
| 116 | + | |
| 117 | + public static Integer getWeek(Date date) { | |
| 118 | + if (date == null) { | |
| 119 | + return null; | |
| 120 | + } | |
| 121 | + Date now = new Date(); | |
| 122 | + date = getDayStartTime(date); | |
| 123 | + long m = now.getTime() - date.getTime() + 86400000L; | |
| 124 | + Long week = m / 604800000L; | |
| 125 | + return week.intValue(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + public static Integer getWeek(Date start, Date end) { | |
| 129 | + if (start == null || end == null) { | |
| 130 | + return null; | |
| 131 | + } | |
| 132 | + start = getDayStartTime(start); | |
| 133 | + end = getDayStartTime(end); | |
| 134 | + long m = end.getTime() - start.getTime() + 86400000L; | |
| 135 | + Long week = m / 604800000L; | |
| 136 | + return week.intValue(); | |
| 137 | + } | |
| 138 | + | |
| 139 | + public static Long getStartTime() { | |
| 140 | + Calendar todayStart = Calendar.getInstance(); | |
| 141 | + todayStart.set(Calendar.HOUR_OF_DAY, 0); | |
| 142 | + todayStart.set(Calendar.MINUTE, 0); | |
| 143 | + todayStart.set(Calendar.SECOND, 0); | |
| 144 | + todayStart.set(Calendar.MILLISECOND, 0); | |
| 145 | + return todayStart.getTime().getTime(); | |
| 146 | + } | |
| 147 | + | |
| 148 | + public static Long getEndTime() { | |
| 149 | + Calendar todayEnd = Calendar.getInstance(); | |
| 150 | + todayEnd.set(Calendar.HOUR_OF_DAY, 23); | |
| 151 | + todayEnd.set(Calendar.MINUTE, 59); | |
| 152 | + todayEnd.set(Calendar.SECOND, 59); | |
| 153 | + todayEnd.set(Calendar.MILLISECOND, 999); | |
| 154 | + return todayEnd.getTime().getTime(); | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * 获取到秒 | |
| 159 | + * | |
| 160 | + * @param date | |
| 161 | + * @return | |
| 162 | + */ | |
| 163 | + public static Integer getSecond(Date date) { | |
| 164 | + if (null == date) { | |
| 165 | + return 0; | |
| 166 | + } | |
| 167 | + long time = date.getTime(); | |
| 168 | + return (int) (time / 1000); | |
| 169 | + } | |
| 170 | + | |
| 171 | + public static Integer getInt() { | |
| 172 | + long time = new Date().getTime(); | |
| 173 | + return (int) (time / 1000); | |
| 174 | + } | |
| 175 | + | |
| 176 | + public static String getYmdhm(int day, int minute) { | |
| 177 | + DateTime dt = new DateTime(); | |
| 178 | + dt = dt.minusMillis(dt.getMillisOfDay()); | |
| 179 | + dt = dt.plusDays(day); | |
| 180 | + dt = dt.plusMinutes(minute); | |
| 181 | + return dt.toString("yyyyMMddHHmm"); | |
| 182 | + } | |
| 183 | + | |
| 184 | + public static Date getYmdhmDate(String s) { | |
| 185 | + if (s == null) { | |
| 186 | + return null; | |
| 187 | + } | |
| 188 | + try { | |
| 189 | + return y_m_d_h_m.parse(s); | |
| 190 | + } catch (Exception e) { | |
| 191 | + return null; | |
| 192 | + } | |
| 193 | + } | |
| 194 | + | |
| 195 | + public static String getymdhm(Date d) { | |
| 196 | + if (d == null) { | |
| 197 | + return null; | |
| 198 | + } | |
| 199 | + try { | |
| 200 | + return y_m_d_h_m.format(d); | |
| 201 | + } catch (Exception e) { | |
| 202 | + return null; | |
| 203 | + } | |
| 204 | + } | |
| 205 | + | |
| 206 | + /** | |
| 207 | + * @return 返回当前年月日字符串 :20131025 | |
| 208 | + */ | |
| 209 | + public static String getYmd() { | |
| 210 | + return ymd.format(new Date()); | |
| 211 | + } | |
| 212 | + | |
| 213 | + public static String getYmd(Date d) { | |
| 214 | + if (d == null) { | |
| 215 | + return null; | |
| 216 | + } | |
| 217 | + try { | |
| 218 | + return ymd.format(d); | |
| 219 | + } catch (Exception e) { | |
| 220 | + return null; | |
| 221 | + } | |
| 222 | + } | |
| 223 | + | |
| 224 | + public static Date parseYMD(String s) { | |
| 225 | + if (s == null) { | |
| 226 | + return null; | |
| 227 | + } | |
| 228 | + | |
| 229 | + try { | |
| 230 | + lock.lock(); | |
| 231 | + return y_m_d.parse(s); | |
| 232 | + } catch (Exception e) { | |
| 233 | + return null; | |
| 234 | + } finally { | |
| 235 | + lock.unlock(); | |
| 236 | + } | |
| 237 | + } | |
| 238 | + | |
| 239 | + public static Date parseYMDEnd(String s) { | |
| 240 | + if (s == null) { | |
| 241 | + return null; | |
| 242 | + } | |
| 243 | + | |
| 244 | + try { | |
| 245 | + lock.lock(); | |
| 246 | + Date d = y_m_d.parse(s); | |
| 247 | + long resultMis = d.getTime() + (1000 * 60 * 60 * 24 - 1); | |
| 248 | + return new Date(resultMis); | |
| 249 | + } catch (Exception e) { | |
| 250 | + return null; | |
| 251 | + } finally { | |
| 252 | + lock.unlock(); | |
| 253 | + } | |
| 254 | + } | |
| 255 | + | |
| 256 | + public static Date parseMD(String s) { | |
| 257 | + if (s == null) { | |
| 258 | + return null; | |
| 259 | + } | |
| 260 | + try { | |
| 261 | + return md.parse(s); | |
| 262 | + } catch (Exception e) { | |
| 263 | + return null; | |
| 264 | + } | |
| 265 | + } | |
| 266 | + | |
| 267 | + public static Date parseD(String s) { | |
| 268 | + if (s == null) { | |
| 269 | + return null; | |
| 270 | + } | |
| 271 | + try { | |
| 272 | + return dd.parse(s); | |
| 273 | + } catch (Exception e) { | |
| 274 | + return null; | |
| 275 | + } | |
| 276 | + } | |
| 277 | + | |
| 278 | + public static String getyyyy_MM_dd(Date d) { | |
| 279 | + if (d == null) { | |
| 280 | + return null; | |
| 281 | + } | |
| 282 | + try { | |
| 283 | + return y_m_d.format(d); | |
| 284 | + } catch (Exception e) { | |
| 285 | + return null; | |
| 286 | + } | |
| 287 | + } | |
| 288 | + | |
| 289 | + public static String getMMdd(Date d) { | |
| 290 | + if (d == null) { | |
| 291 | + return null; | |
| 292 | + } | |
| 293 | + try { | |
| 294 | + return md.format(d); | |
| 295 | + } catch (Exception e) { | |
| 296 | + return null; | |
| 297 | + } | |
| 298 | + } | |
| 299 | + | |
| 300 | + public static String getMM_dd(Date d) { | |
| 301 | + if (d == null) { | |
| 302 | + return null; | |
| 303 | + } | |
| 304 | + try { | |
| 305 | + return m_d.format(d); | |
| 306 | + } catch (Exception e) { | |
| 307 | + return null; | |
| 308 | + } | |
| 309 | + } | |
| 310 | + | |
| 311 | + public static String getDD(Date d) { | |
| 312 | + if (d == null) { | |
| 313 | + return null; | |
| 314 | + } | |
| 315 | + try { | |
| 316 | + return dd.format(d); | |
| 317 | + } catch (Exception e) { | |
| 318 | + return null; | |
| 319 | + } | |
| 320 | + } | |
| 321 | + | |
| 322 | + public static Date parseYMDHMS(String s) { | |
| 323 | + if (s == null) { | |
| 324 | + return null; | |
| 325 | + } | |
| 326 | + try { | |
| 327 | + return y_m_d_h_m_s.parse(s); | |
| 328 | + } catch (Exception e) { | |
| 329 | + return null; | |
| 330 | + } | |
| 331 | + } | |
| 332 | + | |
| 333 | + public static Date parseYMDHM(String s) { | |
| 334 | + if (s == null) { | |
| 335 | + return null; | |
| 336 | + } | |
| 337 | + try { | |
| 338 | + return y_m_d_h_m1.parse(s); | |
| 339 | + } catch (Exception e) { | |
| 340 | + return null; | |
| 341 | + } | |
| 342 | + } | |
| 343 | + | |
| 344 | + public static String getyyyy_MM_dd_hms(Date d) { | |
| 345 | + if (d == null) { | |
| 346 | + return null; | |
| 347 | + } | |
| 348 | + try { | |
| 349 | + return y_m_d_h_m_s.format(d); | |
| 350 | + } catch (Exception e) { | |
| 351 | + return null; | |
| 352 | + } | |
| 353 | + } | |
| 354 | + | |
| 355 | + public static Date getDayStartTime(Date startTime) { | |
| 356 | + if (startTime == null) { | |
| 357 | + return null; | |
| 358 | + } | |
| 359 | + DateTime dt = new DateTime(startTime.getTime()); | |
| 360 | + dt = dt.minusMillis(dt.getMillisOfDay()); | |
| 361 | + return dt.toDate(); | |
| 362 | + } | |
| 363 | + | |
| 364 | + public static Date getDayEndTime(Date endTime) { | |
| 365 | + if (endTime == null) { | |
| 366 | + return null; | |
| 367 | + } | |
| 368 | + DateTime dt = new DateTime(endTime.getTime()); | |
| 369 | + dt = dt.minusMillis(dt.getMillisOfDay()).plusSeconds(DAY_SECONDS); | |
| 370 | + return dt.toDate(); | |
| 371 | + } | |
| 372 | + | |
| 373 | + public static Date getMonthStart(Date time) { | |
| 374 | + if (time == null) { | |
| 375 | + return null; | |
| 376 | + } | |
| 377 | + DateTime dt = new DateTime(time.getTime()); | |
| 378 | + dt = dt.minusMillis(dt.getMillisOfDay()).minusDays(dt.getDayOfMonth() - 1); | |
| 379 | + return dt.toDate(); | |
| 380 | + } | |
| 381 | + | |
| 382 | + public static Date getMonthEndTime(Date endTime) { | |
| 383 | + if (endTime == null) { | |
| 384 | + return null; | |
| 385 | + } | |
| 386 | + DateTime dt = new DateTime(getMonthStart(new DateTime(endTime.getTime()).plusMonths(1).toDate())).minusDays(1) | |
| 387 | + .plusSeconds(DAY_SECONDS); | |
| 388 | + return dt.toDate(); | |
| 389 | + } | |
| 390 | + | |
| 391 | + public static Date getQuarterStart(Date date) { | |
| 392 | + if (date == null) { | |
| 393 | + return null; | |
| 394 | + } | |
| 395 | + DateTime dt = new DateTime(date.getTime()).minusMonths(1); | |
| 396 | + dt = dt.withMonthOfYear(dt.getMonthOfYear() / 3 * 3 + 1); | |
| 397 | + return getMonthStart(dt.toDate()); | |
| 398 | + } | |
| 399 | + | |
| 400 | + public static Date getQuarterEnd(Date date) { | |
| 401 | + if (date == null) { | |
| 402 | + return null; | |
| 403 | + } | |
| 404 | + DateTime dt = new DateTime(date.getTime()).minusMonths(1); | |
| 405 | + dt = dt.withMonthOfYear(dt.getMonthOfYear() / 3 * 3 + 3); | |
| 406 | + return getMonthEndTime(dt.toDate()); | |
| 407 | + } | |
| 408 | + | |
| 409 | + public static Date getWeekStart(Date date) { | |
| 410 | + if (date == null) { | |
| 411 | + return null; | |
| 412 | + } | |
| 413 | + DateTime dt = new DateTime(date.getTime()); | |
| 414 | + dt = dt.withDayOfWeek(1); | |
| 415 | + return getDayStartTime(dt.toDate()); | |
| 416 | + } | |
| 417 | + | |
| 418 | + public static Date getWeekEnd(Date date) { | |
| 419 | + if (date == null) { | |
| 420 | + return null; | |
| 421 | + } | |
| 422 | + DateTime dt = new DateTime(date.getTime()); | |
| 423 | + dt = dt.withDayOfWeek(1).plusDays(6); | |
| 424 | + return getDayEndTime(dt.toDate()); | |
| 425 | + } | |
| 426 | + | |
| 427 | + public static Integer getWeekNum(Date date) { | |
| 428 | + if (date == null) { | |
| 429 | + return null; | |
| 430 | + } | |
| 431 | + DateTime dt = new DateTime(date.getTime()); | |
| 432 | + return (dt.getWeekyear() % 100) * 100 + dt.getWeekOfWeekyear(); | |
| 433 | + } | |
| 434 | + | |
| 435 | + public static Date addDay(Date srcDate, int days) { | |
| 436 | + Calendar rightNow = Calendar.getInstance(); | |
| 437 | + rightNow.setTime(srcDate); | |
| 438 | + rightNow.add(Calendar.DAY_OF_YEAR, days); | |
| 439 | + return rightNow.getTime(); | |
| 440 | + } | |
| 441 | + | |
| 442 | + public static Date addMonth(Date srcDate, int month) { | |
| 443 | + Calendar rightNow = Calendar.getInstance(); | |
| 444 | + rightNow.setTime(srcDate); | |
| 445 | + rightNow.add(Calendar.MONTH, month); | |
| 446 | + return rightNow.getTime(); | |
| 447 | + } | |
| 448 | + | |
| 449 | + public static Date addYear(Date srcDate, int year) { | |
| 450 | + Calendar rightNow = Calendar.getInstance(); | |
| 451 | + rightNow.setTime(srcDate); | |
| 452 | + rightNow.add(Calendar.YEAR, year); | |
| 453 | + return rightNow.getTime(); | |
| 454 | + } | |
| 455 | + | |
| 456 | + public static Date addWeek(Date srcDate, int weak) { | |
| 457 | + Calendar rightNow = Calendar.getInstance(); | |
| 458 | + rightNow.setTime(srcDate); | |
| 459 | + rightNow.add(Calendar.WEEK_OF_YEAR, weak); | |
| 460 | + return rightNow.getTime(); | |
| 461 | + } | |
| 462 | + | |
| 463 | + /** | |
| 464 | + * 获取当天还剩多少秒 | |
| 465 | + * | |
| 466 | + * @return | |
| 467 | + */ | |
| 468 | + public static int getDaySeconds() { | |
| 469 | + Calendar curDate = Calendar.getInstance(); | |
| 470 | + | |
| 471 | + Calendar tommorowDate = new GregorianCalendar(curDate.get(Calendar.YEAR), curDate.get(Calendar.MONTH), | |
| 472 | + curDate.get(Calendar.DATE) + 1, 0, 0, 0); | |
| 473 | + return (int) (tommorowDate.getTimeInMillis() - curDate.getTimeInMillis()) / 1000; | |
| 474 | + } | |
| 475 | + | |
| 476 | + /** | |
| 477 | + * 计算儿童的月龄 | |
| 478 | + * @param birth | |
| 479 | + * @param checkDate | |
| 480 | + * @return | |
| 481 | + */ | |
| 482 | + public static String getBabyMonthAge(Date birth, Date checkDate) { | |
| 483 | + Calendar rightNow = Calendar.getInstance(); | |
| 484 | + rightNow.setTime(checkDate); | |
| 485 | + rightNow.add(Calendar.MONTH, -1); | |
| 486 | + if (birth.getTime() > rightNow.getTime().getTime()) { | |
| 487 | + int day = daysBetween(birth, checkDate); | |
| 488 | + return day + "天"; | |
| 489 | + } | |
| 490 | + Calendar rightNow1 = Calendar.getInstance(); | |
| 491 | + rightNow1.setTime(checkDate); | |
| 492 | + rightNow1.add(Calendar.YEAR, -1); | |
| 493 | + | |
| 494 | + if (birth.getTime() > rightNow1.getTime().getTime()) { | |
| 495 | + | |
| 496 | + int month = getMonthSpace(birth, checkDate); | |
| 497 | + Calendar yearNow = Calendar.getInstance(); | |
| 498 | + yearNow.setTime(birth); | |
| 499 | + yearNow.add(Calendar.MONTH, month); | |
| 500 | + | |
| 501 | + int day = daysBetween(yearNow.getTime(), checkDate); | |
| 502 | + String dstr = ""; | |
| 503 | + if (day != 0) { | |
| 504 | + dstr = day + "天"; | |
| 505 | + } | |
| 506 | + String ms = ""; | |
| 507 | + if (month != 0) { | |
| 508 | + ms = month + "月龄"; | |
| 509 | + } | |
| 510 | + return ms + dstr; | |
| 511 | + } | |
| 512 | + int year = getYearSpace(birth, checkDate); | |
| 513 | + rightNow.setTime(checkDate); | |
| 514 | + rightNow.add(Calendar.YEAR, year); | |
| 515 | + | |
| 516 | + int month = getMonthSpace1(birth, rightNow.getTime()); | |
| 517 | + | |
| 518 | + return year + "岁" + (month == 0 ? "" : month + "月龄"); | |
| 519 | + } | |
| 520 | + | |
| 521 | + public static int daysBetween(Date smdate, Date bdate) { | |
| 522 | + if (smdate == null || bdate == null) { | |
| 523 | + return -1; | |
| 524 | + } | |
| 525 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | |
| 526 | + try { | |
| 527 | + smdate = sdf.parse(sdf.format(smdate)); | |
| 528 | + bdate = sdf.parse(sdf.format(bdate)); | |
| 529 | + Calendar cal = Calendar.getInstance(); | |
| 530 | + cal.setTime(smdate); | |
| 531 | + long time1 = cal.getTimeInMillis(); | |
| 532 | + cal.setTime(bdate); | |
| 533 | + long time2 = cal.getTimeInMillis(); | |
| 534 | + long between_days = (time2 - time1) / (1000 * 3600 * 24); | |
| 535 | + return Integer.parseInt(String.valueOf(between_days)); | |
| 536 | + } catch (ParseException e) { | |
| 537 | + e.printStackTrace(); | |
| 538 | + } | |
| 539 | + | |
| 540 | + return 0; | |
| 541 | + } | |
| 542 | + | |
| 543 | + public static int getMonthSpace(Date date1, Date date2) { | |
| 544 | + | |
| 545 | + int result = 0; | |
| 546 | + | |
| 547 | + Calendar c1 = Calendar.getInstance(); | |
| 548 | + Calendar c2 = Calendar.getInstance(); | |
| 549 | + | |
| 550 | + c1.setTime(date1); | |
| 551 | + c2.setTime(date2); | |
| 552 | + | |
| 553 | + result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH); | |
| 554 | + if (result <= 0) { | |
| 555 | + result = 12 - Math.abs(result); | |
| 556 | + } | |
| 557 | + | |
| 558 | + // result = result == 0 ? 1 : Math.abs(result); | |
| 559 | + if (Integer.parseInt(getDD(date1)) > Integer.parseInt(getDD(date2))) { | |
| 560 | + result = result - 1; | |
| 561 | + } | |
| 562 | + | |
| 563 | + return result; | |
| 564 | + | |
| 565 | + } | |
| 566 | + | |
| 567 | + public static int countMonths(Date date1, Date date2) throws ParseException { | |
| 568 | + | |
| 569 | + /* Calendar c1=Calendar.getInstance(); | |
| 570 | + Calendar c2=Calendar.getInstance(); | |
| 571 | + | |
| 572 | + c1.setTime(date1); | |
| 573 | + c2.setTime(date2); | |
| 574 | + | |
| 575 | + int year =c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR); | |
| 576 | + System.out.print(year); | |
| 577 | + //开始日期若小月结束日期 | |
| 578 | + if(year<0){ | |
| 579 | + year=-year; | |
| 580 | + return year*12+c1.get(Calendar.MONTH)-c2.get(Calendar.MONTH); | |
| 581 | + }*/ | |
| 582 | + | |
| 583 | + // return year*12+c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH); | |
| 584 | + return 0; | |
| 585 | + | |
| 586 | + } | |
| 587 | + | |
| 588 | + public static int getBabyMonthAge1(Date birth, Date checkDate) { | |
| 589 | + Calendar rightNow = Calendar.getInstance(); | |
| 590 | + rightNow.setTime(checkDate); | |
| 591 | + rightNow.add(Calendar.MONTH, -1); | |
| 592 | + if (birth.getTime() > rightNow.getTime().getTime()) { | |
| 593 | + return 0; | |
| 594 | + } | |
| 595 | + Calendar rightNow1 = Calendar.getInstance(); | |
| 596 | + rightNow1.setTime(checkDate); | |
| 597 | + rightNow1.add(Calendar.YEAR, -1); | |
| 598 | + | |
| 599 | + if (birth.getTime() > rightNow1.getTime().getTime()) { | |
| 600 | + | |
| 601 | + int month = getMonthSpace(birth, checkDate); | |
| 602 | + Calendar yearNow = Calendar.getInstance(); | |
| 603 | + yearNow.setTime(birth); | |
| 604 | + yearNow.add(Calendar.MONTH, month); | |
| 605 | + return month; | |
| 606 | + } | |
| 607 | + int year = getYearSpace(birth, checkDate); | |
| 608 | + rightNow.setTime(checkDate); | |
| 609 | + rightNow.add(Calendar.YEAR, year); | |
| 610 | + | |
| 611 | + int month = getMonthSpace1(birth, rightNow.getTime()); | |
| 612 | + | |
| 613 | + return year * 12 + month; | |
| 614 | + } | |
| 615 | + | |
| 616 | + public static int getMonthSpace1(Date date1, Date date2) { | |
| 617 | + | |
| 618 | + int result = 0; | |
| 619 | + | |
| 620 | + Calendar c1 = Calendar.getInstance(); | |
| 621 | + Calendar c2 = Calendar.getInstance(); | |
| 622 | + | |
| 623 | + c1.setTime(date1); | |
| 624 | + c2.setTime(date2); | |
| 625 | + | |
| 626 | + result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH); | |
| 627 | + if (result < 0) { | |
| 628 | + result = 12 - Math.abs(result); | |
| 629 | + } | |
| 630 | + | |
| 631 | + if (Integer.parseInt(getDD(date1)) > Integer.parseInt(getDD(date2))) { | |
| 632 | + result = result - 1; | |
| 633 | + } | |
| 634 | + | |
| 635 | + return result; | |
| 636 | + | |
| 637 | + } | |
| 638 | + | |
| 639 | + public static int getYearSpace(Date date1, Date date2) { | |
| 640 | + int result = 0; | |
| 641 | + | |
| 642 | + Calendar c1 = Calendar.getInstance(); | |
| 643 | + Calendar c2 = Calendar.getInstance(); | |
| 644 | + | |
| 645 | + c1.setTime(date1); | |
| 646 | + c2.setTime(date2); | |
| 647 | + | |
| 648 | + result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR); | |
| 649 | + if (Integer.parseInt(getMMdd(date1).split("-")[0]) > Integer.parseInt(getMMdd(date2).split("-")[0])) { | |
| 650 | + result = result - 1; | |
| 651 | + } | |
| 652 | + return result == 0 ? 1 : Math.abs(result); | |
| 653 | + | |
| 654 | + } | |
| 655 | + | |
| 656 | + public static int getBabyAgeMonth(Date d1, Date d2) { | |
| 657 | + int months = 0;//相差月份 | |
| 658 | + int y1 = d1.getYear(); | |
| 659 | + int y2 = d2.getYear(); | |
| 660 | + int dd1 = d1.getDate(); //起始日期天 | |
| 661 | + int dd2 = d2.getDate(); //结束日期天 | |
| 662 | + if (d1.getTime() < d2.getTime()) { | |
| 663 | + months = d2.getMonth() - d1.getMonth() + (y2 - y1) * 12; | |
| 664 | + if (dd2 < dd1) { | |
| 665 | + months = months - 1; | |
| 666 | + } | |
| 667 | + } | |
| 668 | + return months; | |
| 669 | + } | |
| 670 | + | |
| 671 | + /** | |
| 672 | + * 获取当前日期是星期几<br> | |
| 673 | + * | |
| 674 | + * @param dt | |
| 675 | + * @return 当前日期是星期几 | |
| 676 | + */ | |
| 677 | + public static String getWeekOfDate(Date dt) { | |
| 678 | + String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; | |
| 679 | + Calendar cal = Calendar.getInstance(); | |
| 680 | + cal.setTime(dt); | |
| 681 | + System.out.println(cal.get(Calendar.DAY_OF_WEEK)); | |
| 682 | + int w = cal.get(Calendar.DAY_OF_WEEK) - 1; | |
| 683 | + if (w < 0) | |
| 684 | + w = 0; | |
| 685 | + return weekDays[w]; | |
| 686 | + } | |
| 687 | + | |
| 688 | + public static Date getRangeDate(Date initDate, int num, String type, int delayDay) { | |
| 689 | + | |
| 690 | + Calendar rightNow = Calendar.getInstance(); | |
| 691 | + rightNow.setTime(initDate); | |
| 692 | + if (type == null) | |
| 693 | + return null; | |
| 694 | + if (type.equals("年".trim())) { | |
| 695 | + rightNow.add(Calendar.YEAR, num); | |
| 696 | + | |
| 697 | + } else if (type.equals("月".trim())) { | |
| 698 | + | |
| 699 | + rightNow.add(Calendar.MONTH, num); | |
| 700 | + } else if (type.equals("周".trim())) { | |
| 701 | + rightNow.add(Calendar.WEEK_OF_YEAR, num); | |
| 702 | + } else if (type.equals("天".trim())) { | |
| 703 | + rightNow.add(Calendar.DAY_OF_YEAR, num); | |
| 704 | + } | |
| 705 | + rightNow.add(Calendar.DAY_OF_YEAR, delayDay); | |
| 706 | + String date = y_m_d.format(rightNow.getTime()); | |
| 707 | + System.out.println(date); | |
| 708 | + return rightNow.getTime(); | |
| 709 | + } | |
| 710 | + | |
| 711 | + public static Date getNewDate(int num, String type, int day) { | |
| 712 | + Date dt = new Date(); | |
| 713 | + | |
| 714 | + Calendar rightNow = Calendar.getInstance(); | |
| 715 | + rightNow.setTime(dt); | |
| 716 | + if (type == null) | |
| 717 | + return null; | |
| 718 | + if (type.trim().equals("年")) { | |
| 719 | + rightNow.add(Calendar.YEAR, num); | |
| 720 | + } else if (type.trim().equals("月")) { | |
| 721 | + rightNow.add(Calendar.MONTH, num); | |
| 722 | + } else if (type.trim().equals("周")) { | |
| 723 | + rightNow.add(Calendar.WEEK_OF_YEAR, num); | |
| 724 | + } else if (type.trim().equals("天")) { | |
| 725 | + rightNow.add(Calendar.DAY_OF_YEAR, num); | |
| 726 | + } | |
| 727 | + rightNow.add(Calendar.DAY_OF_YEAR, day); | |
| 728 | + String date = y_m_d.format(rightNow.getTime()); | |
| 729 | + System.out.println(date); | |
| 730 | + return rightNow.getTime(); | |
| 731 | + } | |
| 732 | + | |
| 733 | + public static Date formatDate(Date date) { | |
| 734 | + String time = y_m_d.format(date); | |
| 735 | + return parseYMD(time); | |
| 736 | + } | |
| 737 | + | |
| 738 | + public static String formatSimpleDate(Date date) { | |
| 739 | + if (date == null) { | |
| 740 | + return ""; | |
| 741 | + } | |
| 742 | + SimpleDateFormat md = new SimpleDateFormat("M-d"); | |
| 743 | + return md.format(date); | |
| 744 | + } | |
| 745 | + | |
| 746 | + public static void main(String[] arg) { | |
| 747 | + System.out.println(getAge(parseYMD("1990-01-9"))); | |
| 748 | + } | |
| 749 | + | |
| 750 | + /** | |
| 751 | + * @auther HuJiaqi | |
| 752 | + * @createTime 2016年11月29日 11时30分 | |
| 753 | + * @discription 这个方法是为了孕产妇围产管理-统计管理-预产期统计管理定制,请慎重使用 | |
| 754 | + */ | |
| 755 | + public static String addMonthWithout0(String date, int add) { | |
| 756 | + int year = Integer.valueOf(date.substring(0, 4)); | |
| 757 | + int month = Integer.valueOf(date.substring(5)) + add; | |
| 758 | + if (month < 13) { | |
| 759 | + date = year + "-" + month; | |
| 760 | + } else { | |
| 761 | + date = (year + 1) + "-" + (month - 12); | |
| 762 | + } | |
| 763 | + return date; | |
| 764 | + } | |
| 765 | + | |
| 766 | + /** | |
| 767 | + * @auther HuJiaqi | |
| 768 | + * @createTime 2016年11月29日 11时30分 | |
| 769 | + * @discription 这个方法是为了孕产妇围产管理-统计管理-预产期统计管理定制,请慎重使用 | |
| 770 | + */ | |
| 771 | + public static String addMonthWith0(String date, int add) { | |
| 772 | + date = addMonthWithout0(date, add); | |
| 773 | + if (date.substring(5).length() == 1) { | |
| 774 | + date = date.substring(0, 4) + "-" + 0 + date.substring(5); | |
| 775 | + } | |
| 776 | + return date; | |
| 777 | + } | |
| 778 | + | |
| 779 | + /** | |
| 780 | + * @auther HuJiaqi | |
| 781 | + * @createTime 2016年12月13日 17时10分 | |
| 782 | + * @discription 格式化传入时间的方法,注意这里是获取的前面时间的00和后面时间的59,这个方法很苛刻,必须前后都传了值,而且是2016-12-15+-+2016-12-25格式 | |
| 783 | + */ | |
| 784 | + public static Date[] getSNDate(String snDateString) { | |
| 785 | + return new Date[] { parseYMD(snDateString.substring(0, 10)), parseYMDEnd(snDateString.substring(13, 23)) }; | |
| 786 | + } | |
| 787 | + | |
| 788 | +} |