| | |
age compute using calendar class [problem]
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
i just can't figure out what's wrong with this.
1. suppose i enter 9-3-1991 as the birthdate, this should return 18. but it returns 17. //wrong
2. suppose i enter 2-9-1991 as the birthdate, this returns 18. //correct
what's wrong with my code?
1. suppose i enter 9-3-1991 as the birthdate, this should return 18. but it returns 17. //wrong
2. suppose i enter 2-9-1991 as the birthdate, this returns 18. //correct
what's wrong with my code?
java Syntax (Toggle Plain Text)
private static int ageCalc(int mm, int dd, int yyyy) { Calendar currentDate = Calendar.getInstance(); Calendar birthDate = new GregorianCalendar(); birthDate.set(yyyy, mm, dd); age = currentDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR); birthDate.add(Calendar.YEAR, age); if(currentDate.before(birthDate)) { age--; } return age; }
a joke.
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
i just can't figure out what's wrong with this.
1. suppose i enter 9-3-1991 as the birthdate, this should return 18. but it returns 17. //wrong
Java Syntax (Toggle Plain Text)
private static int ageCalc(int mm, int dd, int yyyy){ // code birthDate.set(yyyy, mm, dd);
I'd have to see the function call, but if you are doing this:
Java Syntax (Toggle Plain Text)
ageCalc (9, 3, 1991)
and passing 9 to the function as mm, realize that 9 is October, not September. January to December are 0 to 11, not 1 to 12.
http://java.sun.com/javase/6/docs/ap...ndar.SEPTEMBER
If you scroll to SEPTEMBER, you'll see that it is equal to 8.
![]() |
Similar Threads
- Pointer to inherited class problem (C++)
- for class: problem with class/ constructor loop (C++)
- date comparison (JSP)
- Abstract class homework problem (C++)
- I want to Edit Calendar Class in ASP.NET (ASP.NET)
- gtk.ListStore / gtk.ComboBoxEntry class problem (Python)
- Simple array/class problem (dot operator)??? (C++)
- help with sort using Calendar class getting null pointer exception (Java)
Other Threads in the Java Forum
- Previous Thread: Client and server with objects
- Next Thread: index on record
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation banking binary bluetooth character chat class classes client code component constructor crashcourse database design developmenthelp draw eclipse error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide if_statement image input integer interface j2me java javaarraylist javadoc javaee javamicroeditionuseofmotionsensor javaprojects jetbrains jmf jni jpanel julia linux list loop map method methods mobile multithreading netbeans newbie number object oracle pearl print printing problem program programming project recursion remove scanner screen server set size sms socket software sort splash sql stop string swing test textfield thread threads time tree validation windows






