How to add N weeks to the current date in Java

1 Answer

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

public class DateModifier {

    /**
     * Adds a specified number of weeks to the current system date.
     *
     * @param weeks The number of weeks to add (supports negative numbers to subtract).
     * @return LocalDate The resulting future date as an immutable object.
     */
    public static LocalDate addWeeksToCurrentDate(long weeks) {
        // 1. Get the current date using the system clock in the default time-zone
        LocalDate currentDate = LocalDate.now();
        
        // 2. Add the specified weeks (plusWeeks handles month/year rollovers automatically)
        return currentDate.plusWeeks(weeks);
    }

    public static void main(String[] args) {
        long weeksToAdd = 4;

        // Calculate the future date
        LocalDate futureDate = addWeeksToCurrentDate(weeksToAdd);

        // Format the date into standard ISO-8601 (YYYY-MM-DD)
        String formattedDate = futureDate.format(DateTimeFormatter.ISO_LOCAL_DATE);

        System.out.println("Adding " + weeksToAdd + " weeks to today's date gives: " + formattedDate);
    }
}


/*
run:

Adding 4 weeks to today's date gives: 2026-12-05

*/

 



answered 1 day ago by avibootz
...