W3docs

How to add one day to a date?

There are a few different ways to add one day to a date.

There are a few different ways to add one day to a date in Java. Here are the most common approaches:

  1. If you're working with the java.time API (introduced in Java 8), you can use the LocalDate class and its plusDays() method to add one day to a date. For example:

import java.time.LocalDate;

LocalDate date = LocalDate.of(2020, 1, 1);
LocalDate newDate = date.plusDays(1);
System.out.println(newDate);  // Output: 2020-01-02
  1. If you just want to add one day to a date represented as a string, you can parse the string into a LocalDate object, add one day using the method above, and then format the date back into a string. For example:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

String dateStr = "2020-01-01";
LocalDate date = LocalDate.parse(dateStr);
LocalDate newDate = date.plusDays(1);
String newDateStr = newDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(newDateStr);  // Output: 2020-01-02
  1. If you just want to add one day to a date represented as a string, and you don't need to do any other date parsing or manipulation, you can simply extract the year, month, and day from the string, add one to the day, and then create a new date string. Note: This naive approach does not handle month or year rollovers (e.g., January 31 → February 1) and should only be used for simple, same-month cases. For example:

String dateStr = "2020-01-01";
String[] parts = dateStr.split("-");
int year = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int day = Integer.parseInt(parts[2]);
day += 1;
String newDateStr = String.format("%04d-%02d-%02d", year, month, day);
System.out.println(newDateStr);  // Output: 2020-01-02