4

I want to add days to a date to get a new date in Java. How to achieve it using the Calendar class.

Calendar dom = new GregorianCalendar(d, m, y);

is the instance of my date of manufacture and I want to reach to date of expiry adding some 100 days to the current date and store it in a variable doe but unable to do that.

stivlo
  • 83,644
  • 31
  • 142
  • 199
terrific
  • 1,637
  • 3
  • 15
  • 14

3 Answers3

10

Make use of Calendar#add(). Here's a kickoff example.

Calendar dom = Calendar.getInstance();
dom.clear();
dom.set(y, m, d); // Note: month is zero based! Subtract with 1 if needed.
Calendar expire = (Calendar) dom.clone();
expire.add(Calendar.DATE, 100);

If you want more flexibility and less verbose code, I'd recommend JodaTime though.

DateTime dom = new DateTime(y, m, d, 0, 0, 0, 0);
DateTime expire = dom.plusDays(100);
stivlo
  • 83,644
  • 31
  • 142
  • 199
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks a ton. But when I try to make new instance of calenar with added value as new date it is not storing it in new instance. – terrific Mar 24 '10 at 12:34
  • 1
    You shouldn't do `new GregorianCalendar(d, m, y)`. Follow the same steps as here above. That's the only right way. Besides, note that you should put `y, m, d` in and not `d, m, y`. Also note that the month is zero based. January is 0, February is 1, March is 2, etc. It's a nightmare, the `Calendar`. – BalusC Mar 24 '10 at 12:50
  • Quick question - Why do you recommend against new GregorianCalendar(...)? – Everyone Mar 24 '10 at 14:21
  • 1
    Two reasons: 1) It's a better practice to program to the interface/abstract class. 2) You really need to `clear()` the calendar to avoid clashes sooner or later when cloning/comparing/etc. – BalusC Mar 24 '10 at 15:08
1

java.time

Now, years later, the old java.util.Date/.Calendar classes are supplanted by the new java.time package in Java 8 and later.

These new classes include a LocalDate class for representing a date-only without time-of-day nor time zone.

LocalDate localDate = LocalDate.of( 2015 , 2 , 3 ) ;
LocalDate later = localDate.plusDays( 100 );

That code above a works for dates. If you instead need to know exact moment of expiration, then you need time-of-day and time zones. In that case, use ZonedDateTime class.

ZoneId zone = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = later.atStartOfDay( zone ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1
    DateFormat formatter = null;
    Date convertedDate = null;
    formatter = new SimpleDateFormat("yyyy-MM-dd");
    try {
        convertedDate = (Date) formatter.parse(pro.getDate().toString());//pro.getDate() is the date getting from database
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(convertedDate);
    cal.add(Calendar.DATE, 7);
        Date cvdate=cal.getTime();
    if (cvdate.after(new Date())){
 //do Something if you show expire...
}
Nandkishor mewara
  • 2,552
  • 16
  • 29