1

Is there a way to add real days to a java.util.Calendar instance?

Example: Calendar instance, date set to 01. Dec. 2015. Now I want to add 31 days to the Calendar, so the resulting date should be 01. Jan. 2016. DAY_OF_YEAR doesn't seems suitable because it represents a day in a year, not a real day.

No duplicate: My demand is to add real days to a Calendar instance and there is no other post which explains the actual work done by Calendar.DATE / Calendar.DAY_OF_YEAR.

Regards

Thomas Schmidt
  • 1,248
  • 2
  • 12
  • 37
  • 1
    possible duplicate of [Adding Days to Calendar](http://stackoverflow.com/questions/18197710/adding-days-to-calendar) – aksamit Aug 12 '15 at 13:11

5 Answers5

4

Maybe add Calendar.DATE?

Calendar cal = Calendar.getInstance();
// add 31 days to the calendar
cal.add(Calendar.DATE, 31);
wawek
  • 1,577
  • 1
  • 13
  • 23
1

See the add(int field, int amount) method in the java.util.Calendar Class.

Example:

Calendar calendar = Calendar.getInstance(); //gets a Calendar Instance with the current Date and Time.
System.out.println("Current Date and Time: " + calendar.getTime());
calendar.add(Calendar.DATE, 31);
System.out.println("Date and Time after adding 31 days: " + calendar.getTime());
Zaid Malhis
  • 588
  • 4
  • 18
1

you can use below code to add real days to calendar:

cal.add(Calendar.DATE, 31);
Nitesh Virani
  • 1,688
  • 4
  • 25
  • 41
1

java 8 having lots of good features http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

code to have some idea:

  import java.time.LocalDate;

    String dt = "2008-01-01";
    int days=31;
    String updateddate=DateIncrementer(dt,days);

    public class DateIncrementer {
      static public String addCustomDay(String date,int noOfDays) {
        return LocalDate.parse(date).plusDays(noOfDays).toString();
      }
    }
Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39
0

DAY_OF_MONTH is a synonym for DATE, so both should do the same. DAY_OF_YEAR is based on the current year, but DAY_OF_MONTH is independent from the current month?

Thomas Schmidt
  • 1,248
  • 2
  • 12
  • 37