1

1.I want to set the setMaxSelectableDate=18years in JDateChooser so i provided it the date by incrementing milliseconds but how should i increment it by 18years.
2.Incrementing by 18years the calculation comes out to be 365*18*24*60*60*1000=56764800000 which gives me error integer number to large.

 Date max=new Date();
Date oth1=new Date(max.getTime() + (365*18*24*60*60*1000));  //days*hours*minutes*seconds*milliseconds
SimpleDateFormat maxdateFormatter1 = new SimpleDateFormat("MMM d,yyyy hh:mm:ss a");
String maxdate=maxdateFormatter1.format(oth1);  
DateChooser_V1.setMaxSelectableDate(new java.util.Date(maxdate));
Abby D
  • 39
  • 1
  • 11

5 Answers5

9

Let java.util.Calendar do this work for you:

Calendar c = Calendar.getInstance();
c.setTime(oldDate);
c.add(Calendar.YEAR, 18);
Date newDate = c.getTime();

Which takes care of leap years, historical GMT offset changes, historical Daylight Saving Time schedule changes etc.

  • i accept the answer its correct but i am not able to do what i need. – Abby D Mar 12 '13 at 21:22
  • @AbbyD What's the problem now? –  Mar 12 '13 at 21:23
  • The problem now seems different than the current question. Consider asking it as a separate question. –  Mar 12 '13 at 21:31
  • i declared **maxdate** at classlevel and initialized it in constructor but it gives me error **Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException at java.util.Date.parse(Date.java:598)**. If i declare and initialize it before constructor than it was running successfully according to my method but now due to c.setTime needs to be implemented in try{}catch{}block i moved my code in constructor and now its giving error. – Abby D Mar 12 '13 at 21:34
  • its working i just pasted my code before initComponents(); the error was created because by mistake i pasted my code after initComponents(); – Abby D Mar 12 '13 at 21:36
1

You need to use a long. You can achieve this by adding an L to your number:

365L* ...

mercutio
  • 1,065
  • 7
  • 18
1

With JodaTime

DateTime in18Years = new DateTime( ).plusYears( 18 );

Here is how to convert to java.util.Date

Date in18Years = new DateTime( ).plusYears( 18 ).toDate( );
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
0

You cannot willy-nilly add seconds (or millseconds) and expect calendar calculations to come out right. Basically it takes some extra effort to account for all of those leap-years, leap seconds, and daylight savings shifts.

Until Java 1.8 comes out, use java.util.Calendar instead of java.util.Date, there are really good reasons that java.util.Date has practically everything in it deprecated. While it looks good in the beginning, with enough use you will find it often "just doesn't work (tm)".

 GregorianCalendar now = new GregorianCalendar();
 now.add(Calendar.YEAR, 18);

And that's assuming that you didn't overflow Integer.MAX_INT.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

I would use a Calendar object to achieve this:

Calendar cal = Calendar.getInstance();
Date dt = new Date();
...
// Set the date value
...
cal.setTime(dt);
cal.add(Calendar.YEAR, +18);
dt = cal.getTime();

Hope this helps you

Barranka
  • 20,547
  • 13
  • 65
  • 83