Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating a future date [duplicate]

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.

like image 507
Troller Avatar asked Sep 11 '13 05:09

Troller


2 Answers

Calendar c = Calendar.getInstance();

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

c.add(Calendar.DATE, 15); // Adds 15 days
like image 139
MansoorShaikh Avatar answered Nov 11 '22 18:11

MansoorShaikh


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

like image 20
Subhrajyoti Majumder Avatar answered Nov 11 '22 17:11

Subhrajyoti Majumder