| | |
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,819
Reputation:
Solved Threads: 501
•
•
•
•
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 |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working






