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.
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 15); // Adds 15 days
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With