Example Uses Of Calendar Class

Write a java program that can find your age if the current year is 2007.

This example uses the Calendar class to compute a person's age. 

// Create a calendar object with the date of birth
Calendar dateOfBirth = new GregorianCalendar(1972, Calendar.JANUARY, 27);

// Create a calendar object with today's date
Calendar today = Calendar.getInstance();

// Get age based on year
int age = today.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR);

// Add the tentative age to the date of birth to get this year's birthday
dateOfBirth.add(Calendar.YEAR, age);

// If this year's birthday has not happened yet, subtract one from age

if (today.before(dateOfBirth)) {
     age--;
}

*-- Kamlesh Bansod

Start Date Example
How to Get the Start Date of the Week in Java

Have a Java Problems
Do you have a Java Question?

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Best Regards,
Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.