LocalDate parsedDate = LocalDate.parse("2015-10-30"); //Parse date from String
LocalDate addedDate = parsedDate.plusDays(1); //Add one to the day field
You can convert in into java.util.Date object as follows.
Date date = Date.from(addedDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
You can formate LocalDate into a String as follows.
LocalDate sourceDate = LocalDate.of(2017, Month.MAY, 27); // Source Date
LocalDate destDate = sourceDate.plusDays(1); // Adding a day to source date.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // Setting date format
String destDate = destDate.format(formatter)); // End date
If you want to use SimpleDateFormat, then do it like this.
String sourceDate = "2017-05-27"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse(sourceDate)); // parsed date and setting to calendar
calendar.add(Calendar.DATE, 1); // number of days to add
String destDate = sdf.format(calendar.getTime()); // End date
In Java 8 simple way to do is:
In java 8 you can use
java.time.LocalDate
You can convert in into
java.util.Date
object as follows.You can formate
LocalDate
into a String as follows.java.time
On Java 8 and later, the java.time package makes this pretty much automatic. (Tutorial)
Assuming
String
input and output:If you are using Java 8, then do it like this.
If you want to use SimpleDateFormat, then do it like this.
If you want to add a single unit of time and you expect that other fields to be incremented as well, you can safely use add method. See example below:
Will Print:
You can use this package from "org.apache.commons.lang3.time":