Code example for GregorianCalendar

Methods: get, setTime

0
	 
	 
	public double calculateAge() 
	{ 
		Calendar now = Calendar.getInstance();
		GregorianCalendar dob = new GregorianCalendar();
		dob.setTime(birthDate);
		 
		int factor = 0;
		if(now.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
		      factor = -1; 
		} 
		 
		int age = now.get(Calendar.YEAR) - dob.get(Calendar.YEAR) + factor;
		System.out.println("Age is: "+age);
		 
		 
		return age;
	} 
	 
	public static void main(String[] args) {