diff --git a/platform-common/src/main/java/com/lyms/platform/common/utils/DateUtil.java b/platform-common/src/main/java/com/lyms/platform/common/utils/DateUtil.java index 209fc8b..5c23662 100644 --- a/platform-common/src/main/java/com/lyms/platform/common/utils/DateUtil.java +++ b/platform-common/src/main/java/com/lyms/platform/common/utils/DateUtil.java @@ -414,8 +414,6 @@ public class DateUtil { if (birth.getTime() > rightNow1.getTime().getTime()) { - int year = getYearSpace(birth, checkDate); - int month = getMonthSpace(birth,checkDate); Calendar yearNow = Calendar.getInstance(); yearNow.setTime(birth); @@ -438,7 +436,7 @@ public class DateUtil { rightNow.setTime(checkDate); rightNow.add(Calendar.YEAR,year); - int month = getMonthSpace(birth,rightNow.getTime()); + int month = getMonthSpace1(birth, rightNow.getTime()); return year+"岁"+ (month == 0 ? "" : month+"月龄"); } @@ -494,6 +492,31 @@ public class DateUtil { } + public static int getMonthSpace1(Date date1, Date date2) { + + int result = 0; + + Calendar c1 = Calendar.getInstance(); + Calendar c2 = Calendar.getInstance(); + + c1.setTime(date1); + c2.setTime(date2); + + result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH); + if (result < 0) + { + result = 12 - Math.abs(result); + } + + if (Integer.parseInt(getDD(date1)) > Integer.parseInt(getDD(date2))) + { + result = result -1; + } + + return result; + + } + public static int getYearSpace(Date date1, Date date2) { @@ -506,7 +529,10 @@ public class DateUtil { c2.setTime(date2); result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR); - + if (Integer.parseInt(getMMdd(date1).split("-")[0]) > Integer.parseInt(getMMdd(date2).split("-")[0])) + { + result = result -1; + } return result == 0 ? 1 : Math.abs(result); } @@ -651,7 +677,7 @@ public class DateUtil { // String st = md.format(new Date()); // System.out.print(st); - String s = getBabyMonthAge(parseYMD("2015-08-19"), new Date()); + String s = getBabyMonthAge(parseYMD("2013-09-16"), new Date()); System.out.println(s); // Date s = addDay(parseYMD("2016-02-26"), 168);