Calculating a date in the future or past

Java:
Calculating a date in the future or past

How to:

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class DateCalculation {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        // Add 10 days to the current date
        LocalDate futureDate = today.plusDays(10);
        System.out.println("Future Date: " + futureDate);

        // Subtract 2 months from the current date
        LocalDate pastDate = today.minus(2, ChronoUnit.MONTHS);
        System.out.println("Past Date: " + pastDate);
    }
}

Output could look like this:

Future Date: 2023-04-30
Past Date: 2023-02-20

Deep Dive

Before Java 8, manipulating dates was a pain. Old classes like java.util.Date and java.util.Calendar were bug-prone and not user-friendly. The java.time package introduced in Java 8 fixed this with well-thought-out classes like LocalDate, LocalTime, and ZonedDateTime.

Alternatives? In the pre-Java 8 era, third-party libraries like Joda-Time were common. Nowadays, you could still use them, but standard java.time is recommended because it’s officially part of Java and handles daylight saving, time zones, and leap years elegantly.

When coding date calculations, consider time zones if your context needs it. For UTC, use Instant instead of LocalDate. For specific zones, you’d typically use ZonedDateTime. Remember, date-time operations can be chained, like date.minusWeeks(1).plusHours(3), making your code cleaner.

See Also

  1. The java.time package overview: Oracle Docs
  2. Time zone handling with ZonedDateTime: Oracle ZonedDateTime
  3. Official date and time patterns for java.time.format.DateTimeFormatter: Oracle DateTimeFormatter