Java 8 added a new API for working with dates and times.
With Java 8 you can use the following lines of code:
// parse date from yyyy-mm-dd pattern
LocalDate januaryFirst = LocalDate.parse("2014-01-01");
// add one day
LocalDate januarySecond = januaryFirst.plusDays(1);
Date today = new Date();
SimpleDateFormat formattedDate = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1); // number of days to add
String tomorrow = (String)(formattedDate.format(c.getTime()));
System.out.println("Tomorrows date is " + tomorrow);
This will give tomorrow's date. c.add(...) parameters could be changed from 1 to another number for appropriate increment.
I prefer to use DateUtils from Apache. Check this http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html. It is handy especially when you have to use it multiple places in your project and would not want to write your one liner method for this.
The API says:
Note that it returns a new Date object and does not make changes to the previous one itself.
Java 8 added a new API for working with dates and times.
With Java 8 you can use the following lines of code:
Please note that this line adds 24 hours:
but this line adds one day
On days with a daylight savings time change (25 or 23 hours) you will get different results!
Construct a Calendar object and use the method add(Calendar.DATE, 1);
This will give tomorrow's date.
c.add(...)
parameters could be changed from 1 to another number for appropriate increment.