-
birthdate class
I need to know how to prompt the user for their birth date and calculate and display how many days old they are. Can anyone help me out, because i am completely stuck, thanks in advance.
-
Do you want user input from the console or from a dialog window?
-
user input from a dialog window would be best.
-
Use the methods of the Date and Calendar classes to represent the birthdate, today, and the difference between the two.
-
import java.util.Calendar;
import java.util.GregorianCalendar;
public class AgeCalculator {
public static void main(String[] args) {
int dob_date = 7;
int dob_month = 7;
int dob_year = 1979;
// Create a calendar object with the DOB
Calendar dob = new GregorianCalendar(dob_year, dob_month, dob_date);
// Create a calendar object with today's date
Calendar today = Calendar.getInstance();
int diff_year = (today.get(Calendar.YEAR) - dob.get(Calendar.YEAR));
int diff_month = (today.get(Calendar.MONTH) + 1) - dob.get(Calendar.MONTH);
int diff_date = (today.get(Calendar.DATE) - dob.get(Calendar.DATE));
int age = 0;
if(diff_year > 0){
age = diff_year;
if(diff_month < 0) age = age - 1;
else if(diff_month == 0){
if(diff_date < 0) age = age - 1;
}
}
System.out.println("Age : " + age);
}
}
Similar Threads
-
By Osiris43 in forum .NET
Replies: 1
Last Post: 08-04-2006, 12:15 PM
-
Replies: 5
Last Post: 01-15-2006, 07:10 PM
-
By none_none in forum Java
Replies: 17
Last Post: 04-28-2005, 03:00 PM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
By Shailesh C.Rathod in forum .NET
Replies: 2
Last Post: 03-13-2002, 07:53 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks