How to calculate the date six months from the current date in Java

1 Answer

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

public class FutureDateCalculator {
    // Function to calculate future date by adding given months
    public static LocalDate calculateFutureDate(int monthsToAdd) {
        return LocalDate.now().plusMonths(monthsToAdd);
    }

    public static void main(String[] args) {
        LocalDate futureDate = calculateFutureDate(6);
        
        // Define the date format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // Print the new date
        System.out.println("Date six months from now: " + futureDate.format(formatter));
    }
}


 
/*
run:
 
Date six months from now: 2025-12-11
 
*/

 



answered Jun 11, 2025 by avibootz

Related questions

...