Here you can find the source of getAge(final Date birthDay, final Date currDate)
Parameter | Description |
---|---|
birthDay | Date of birth |
currDate | Date where the age is to be calculated |
public static int getAge(final Date birthDay, final Date currDate)
//package com.java2s; /**//ww w.ja v a 2s .c o m * * Licensed under the GNU General Public License (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.gnu.org/licenses/gpl.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * * @author Vadim Kisen * * copyright 2010 by uTest */ import java.util.Calendar; import java.util.Date; public class Main { public static final int YEAR = Calendar.YEAR; public static final int HOUR = Calendar.HOUR; public static final int MINUTE = Calendar.MINUTE; public static final int SECOND = Calendar.SECOND; /** * Returns the age in years according to the date of birth passed and the * date passed as current. * * @param birthDay * Date of birth * @param currDate * Date where the age is to be calculated * * @return years old using current time */ public static int getAge(final Date birthDay, final Date currDate) { final Calendar endDate = Calendar.getInstance(); final Calendar startDate = Calendar.getInstance(); startDate.setTime(birthDay); endDate.setTime(currDate); int years = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR); startDate.set(Calendar.YEAR, endDate.get(Calendar.YEAR)); if (startDate.after(endDate)) { years--; } return years; } /** * Given a Date as parameter, it sets it to the specified time and returns a * copy. * * @param date * Original date * @param hours * Hours to set to the date * @param minutes * Minutes to set to the date * @param seconds * Seconds to set to the date * * @return A new Date with the given time. */ public static Date setTime(final Date date, final int hours, final int minutes, final int seconds) { final Calendar aux = Calendar.getInstance(); aux.setTime(date); aux.set(Calendar.MILLISECOND, 0); aux.set(Calendar.HOUR, hours); aux.set(Calendar.MINUTE, minutes); aux.set(Calendar.SECOND, seconds); return aux.getTime(); } }