-3

Below is the code which generates the output as "9/2/2014"

public static void main (String[]args) throws ParseException
{
    java.util.Date d = new Date();
    SimpleDateFormat sd = new SimpleDateFormat("M/d/yyyy");
    System.out.println(sd.format(d));


}

Now i need to add some n no of days and i wanted to get the output as 9/12/2014

please help me ...

Raghuveer
  • 115
  • 2
  • 10

2 Answers2

1

If you want add month, or days to your date, use something like that:

public static Date addDays(Date date, int days)
{
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DATE, days); //minus number would decrement the days
    return cal.getTime();
}

to add month use Calendar.Month

kris14an
  • 741
  • 7
  • 24
0

Calendar has methods for date manipulations. First create Calendar instance and set date to it

 Calendar calendar = Calendar.getInstance();
 calendar.setTime(new Date());

Then you can use calendar instance to add days like

 calendar.add(Calendar.DATE,10);

to get date from calendar, use

 System.out.println(calendar.getTime());
Adi
  • 2,364
  • 1
  • 17
  • 23