Commit 0b23f5b7895e826876e350a767d274ee71fc0484

Authored by liquanyu
1 parent de29a87c4e

月龄计算

Showing 1 changed file with 31 additions and 5 deletions

platform-common/src/main/java/com/lyms/platform/common/utils/DateUtil.java View file @ 0b23f5b
... ... @@ -414,8 +414,6 @@
414 414 if (birth.getTime() > rightNow1.getTime().getTime())
415 415 {
416 416  
417   - int year = getYearSpace(birth, checkDate);
418   -
419 417 int month = getMonthSpace(birth,checkDate);
420 418 Calendar yearNow = Calendar.getInstance();
421 419 yearNow.setTime(birth);
... ... @@ -438,7 +436,7 @@
438 436 rightNow.setTime(checkDate);
439 437 rightNow.add(Calendar.YEAR,year);
440 438  
441   - int month = getMonthSpace(birth,rightNow.getTime());
  439 + int month = getMonthSpace1(birth, rightNow.getTime());
442 440  
443 441 return year+"岁"+ (month == 0 ? "" : month+"月龄");
444 442 }
445 443  
446 444  
... ... @@ -494,8 +492,33 @@
494 492  
495 493 }
496 494  
  495 + public static int getMonthSpace1(Date date1, Date date2) {
497 496  
  497 + int result = 0;
498 498  
  499 + Calendar c1 = Calendar.getInstance();
  500 + Calendar c2 = Calendar.getInstance();
  501 +
  502 + c1.setTime(date1);
  503 + c2.setTime(date2);
  504 +
  505 + result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH);
  506 + if (result < 0)
  507 + {
  508 + result = 12 - Math.abs(result);
  509 + }
  510 +
  511 + if (Integer.parseInt(getDD(date1)) > Integer.parseInt(getDD(date2)))
  512 + {
  513 + result = result -1;
  514 + }
  515 +
  516 + return result;
  517 +
  518 + }
  519 +
  520 +
  521 +
499 522 public static int getYearSpace(Date date1, Date date2) {
500 523 int result = 0;
501 524  
... ... @@ -506,7 +529,10 @@
506 529 c2.setTime(date2);
507 530  
508 531 result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
509   -
  532 + if (Integer.parseInt(getMMdd(date1).split("-")[0]) > Integer.parseInt(getMMdd(date2).split("-")[0]))
  533 + {
  534 + result = result -1;
  535 + }
510 536 return result == 0 ? 1 : Math.abs(result);
511 537  
512 538 }
... ... @@ -651,7 +677,7 @@
651 677 // String st = md.format(new Date());
652 678 // System.out.print(st);
653 679  
654   - String s = getBabyMonthAge(parseYMD("2015-08-19"), new Date());
  680 + String s = getBabyMonthAge(parseYMD("2013-09-16"), new Date());
655 681 System.out.println(s);
656 682  
657 683 // Date s = addDay(parseYMD("2016-02-26"), 168);