Here you can find the source of calculateAge(Date birthDate)
Parameter | Description |
---|---|
birthDate | a parameter |
public static int calculateAge(Date birthDate)
//package com.java2s; /**//from ww w .ja v a2s . c o m * Copyright (C) 2013 Company. All Rights Reserved. * * This software is the proprietary information of Company . * Use is subjected to license terms. * * @since Jul 17, 2013 11:48:25 PM * @author SPA * */ import java.util.Calendar; import java.util.Date; public class Main { /** * * This method is used to calculate Age. * * @param birthDate * @return int. */ public static int calculateAge(Date birthDate) { int age = 0; Calendar c = Calendar.getInstance(); c.setTime(birthDate); age = Calendar.getInstance().get(Calendar.YEAR) - c.get(Calendar.YEAR); // Also need compare month and day if (Calendar.getInstance().get(Calendar.MONTH) < c.get(Calendar.MONTH)) { age--; } else if (Calendar.getInstance().get(Calendar.MONTH) == c.get(Calendar.MONTH) && Calendar.getInstance().get(Calendar.DAY_OF_MONTH) < c.get(Calendar.DAY_OF_MONTH)) { age--; } return age; } }