10

I have created a program to calculate days and in my program i want add 7 more days to the current date.

Which means if today's date is 9/11/2013, i want to make it 9/18/2013 by getting the current date and adding 7 more days to it. How do i implement this.

I know how to get today's date by using the date class but i don't know to add another 7 days to the current date.

This is the method i used to get the current date :

 public void dateCalculator(){
    Date date;
    date=new Date();
           }

Thank you for your time.

Troller
  • 1,108
  • 8
  • 29
  • 47

2 Answers2

24
Calendar c = Calendar.getInstance();

c.setTime(new Date()); // Now use today date.

c.add(Calendar.DATE, 15); // Adds 15 days
MansoorShaikh
  • 913
  • 1
  • 6
  • 19
  • 1
    By default, the new `Calendar` instance will have the current time, and you'll need `getTime()` to get the `Date` out. – lionello Sep 06 '18 at 09:22
18

you can get that by Calendar#add(Calender.DATE,7)

code snippet -

Calendar cal = Calendar.getInstance();
System.out.println("current date: " + cal.getTime());
cal.add(Calendar.DATE, 7);
System.out.println("7 days later: " + cal.getTime());

result -

current date: Tue Sep 10 15:53:17 MST 2013
7 days later: Tue Sep 17 15:53:17 MST 2013

Note: code compiled in - http://www.compileonline.com/compile_java_online.php

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103