0

I have a JDateChooser in my program. Whenever the selects his date of birth from JDateChooser , I want his age to be displayed in a JTextField.

Initially, I tried to make it work with the MouseListener as :

private void jDateChooser1MouseExited(java.awt.event.MouseEvent evt) 
{                                                
  Calendar dob = Calendar.getInstance();  

 //utilDob is a java.util.Date variable which stores date selected by user
 dob.setTime(utilDob);  
 Calendar today = Calendar.getInstance();  
 int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);  
 if (today.get(Calendar.MONTH) < dob.get(Calendar.MONTH))
   age--;  
 else if (today.get(Calendar.MONTH) == dob.get(Calendar.MONTH)
                                && today.get(Calendar.DAY_OF_MONTH) <    dob.get(Calendar.DAY_OF_MONTH)) 
     age--;  


 jTextField11.setText(Integer.toString(age));
 displayAge=Integer.parseInt(jTextField11.getText());
}                               

But, the above mentioned function didn't help me. Is there any other event/action listener I can use?

ninja coder
  • 27
  • 1
  • 8
  • 1
    You didn't tell us the problem, only that it didn't work. Be sure to include a concise question which sums up what has happened and contrast that with what should have happened. – Matt C Mar 09 '16 at 17:27
  • Well, the problem is that the code doesn't calculate age. It seems as if the function doesn't execute in the first place. – ninja coder Mar 09 '16 at 17:29
  • 1
    So it "seems" like it doesn't execute. You should have debugged before coming here... But anyway, put in a print statement in the function somewhere. Just a good ole normal `System.out.println("Whatever");` in the function, then you'll be able to see if the function is being called if you see "Whatever" pop up in the console. – Matt C Mar 09 '16 at 17:32
  • It doesn't print the statement. – ninja coder Mar 09 '16 at 17:47

1 Answers1

0

You are using outmoded classes.

java.time

Java 8 and later comes with the java.time framework built-in.

LocalDate

The LocalDate class represents a date-only value, without time-of-day and without time zone. But time zone is critical it determining "today". For any given moment the date may vary around the world by time zone. A few moments after midnight in Paris is still “yesterday” in Montréal.

ZoneId zoneId = ZoneId.of ( "America/Montreal" );
LocalDate today = LocalDate.now ( zoneId );

Build a date-of-birth from your user input.

LocalDate dob = LocalDate.of ( 1955 , 1 , 2 );

Or parse a string compliant with the ISO 8601 standard.

LocalDate dob = LocalDate.parse( "1955-01-02" );

Or use a DateTimeFormatter to help parse a different string.

If you have a java.util.Date object on hand use a new method added to that old class for conversion to java.time: java.util.Date::toInstant. An Instant is a moment on the timeline in UTC.

Instant instant = myJavaUtilDate.toInstant();

Apple a time zone.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

Extract a LocalDate.

LocalDate localDate = zdt.toLocalDate();

Period

Age is just one kind of elapsed time, a span of time. In java.time, the Period class represents a span of time tracked in terms of years, months, and days.

Period age = Period.between ( dob , today );

Get years from Period

Ask for the years if that is all you want.

int years = age.getYears ();

Beware that getYears is not normalized to the full number of years as you may intuit. Depending on how a Period is constructed, you may get an unexpected result. Another approach is to call toTotalMonths() and divide by 12 (twelve months to a year). See both approaches in this quick example; compare y and y2.

Period p = Period.ofMonths ( 25 );
int y = p.getYears ();
long y2 = ( ( p.toTotalMonths () ) / 12 );
System.out.println ( "p: " + p + " | y: " + y + " | y2: " + y2 );

p: P25M | y: 0 | y2: 2

Even better, force the Period object to be normalized to a full count of years then months then days. Well, not exactly force the object: Because of immutable objects, you are actually instantiating a new object based on the original object’s values.

So while our age object happens to already be normalized because of its particular construction, it might be a good habit to always call normalized.

Period age = Period.between ( dob , today ).normalized();

Dump to console. By default, the Period::toString method generates a string according to the ISO 8601 standard for Durations such as P61Y2M27D seen here.

System.out.println ( "zoneid: " + zoneId + " | today: " + today + " | dob: " + dob + " | age: " + age + " | years: " + years );

zoneid: America/Montreal | today: 2016-03-29 | dob: 1955-01-02 | age: P61Y2M27D | years: 61

Convert int to Integer

Lastly, if you need an object rather than a primitive, convert int to Integer.

Integer yearsObj = Integer.valueOf ( years );

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank u for your response. Well, I have a JDateChooser in my program in which the user will select his/her date of birth and hence I have used java.util.Date. How can I store the date selected from JDatechooser into a LocalDate variable? – ninja coder Mar 29 '16 at 21:49
  • @ninjacoder What object are you getting from that utility? I could not find its JavaDoc nor any guide documentation. – Basil Bourque Mar 30 '16 at 04:22
  • i am using toedter's JDateChooser – ninja coder Mar 30 '16 at 05:12