svnno****@sourc*****
svnno****@sourc*****
2007年 4月 25日 (水) 21:58:38 JST
Revision: 82 http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=pal&view=rev&rev=82 Author: shinsuke Date: 2007-04-25 21:58:38 +0900 (Wed, 25 Apr 2007) Log Message: ----------- added methods to calculate age. Modified Paths: -------------- libraries/common-utils/trunk/src/main/java/jp/sf/pal/common/util/DateUtil.java -------------- next part -------------- Modified: libraries/common-utils/trunk/src/main/java/jp/sf/pal/common/util/DateUtil.java =================================================================== --- libraries/common-utils/trunk/src/main/java/jp/sf/pal/common/util/DateUtil.java 2007-04-25 12:57:05 UTC (rev 81) +++ libraries/common-utils/trunk/src/main/java/jp/sf/pal/common/util/DateUtil.java 2007-04-25 12:58:38 UTC (rev 82) @@ -231,4 +231,32 @@ return cal.get(Calendar.SECOND); } + public static Integer calculateAge(Date date) { + if (date == null) { + return null; + } + Date now = Calendar.getInstance().getTime(); + int age = DateUtil.getYear(now) - DateUtil.getYear(date); + if (age > 0) { + if (DateUtil.getMonth(now) < DateUtil.getMonth(date)) { + if (DateUtil.getDate(now) < DateUtil.getDate(date)) { + age--; + } + } + return new Integer(age); + } + return null; + } + + public static Date calculateDate(Integer age) { + if (age == null) { + return null; + } + Date now = Calendar.getInstance().getTime(); + int year = DateUtil.getYear(now) - age.intValue(); + + return DateUtil.get(year, DateUtil.getMonth(now), + DateUtil.getDate(now), 0, 0, 0, 0); + } + }