A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/localdate-minusyears-method-in-java-with-examples/ below:

LocalDate minusYears() method in Java with Examples

LocalDate minusYears() method in Java with Examples

Last Updated : 07 May, 2019

The

minusYears()

method of a

LocalDate

class is used to subtract the number of specified years from this LocalDate and return a copy of LocalDate. This method subtracts the years field in the following steps:

For example, 2016-02-29 (leap year) minus one year gives date 2015-02-29 but this is an invalid result, so the last valid day of the month, 2015-02-28, is returned. This instance is immutable and unaffected by this method call.

Syntax:
public LocalDate minusYears(long yearsToSubtract)
Parameters:

This method accepts a single parameter

yearsToSubtract

which represents the years to subtract, may be negative.

Return Value:

This method returns a

LocalDate

based on this date with the years subtracted, not null.

Exception:

This method throws

DateTimeException

if the result exceeds the supported date range. Below programs illustrate the minusYears() method:

Program 1: Java
// Java program to demonstrate
// LocalDate.minusYears() method

import java.time.*;

public class GFG {
    public static void main(String[] args)
    {

        // create a LocalDate object
        LocalDate date
            = LocalDate.parse("2020-10-22");

        // print instance
        System.out.println("LocalDate before"
                           + " subtracting years:" + date);

        // subtract 30 years
        LocalDate returnvalue
            = date.minusYears(30);

        // print result
        System.out.println("LocalDate after "
                           + " subtracting years:" + returnvalue);
    }
}
Output:
LocalDate before subtracting years:2020-10-22
LocalDate after  subtracting years:1990-10-22
Program 2: Java
// Java program to demonstrate
// LocalDate.minusYears() method

import java.time.*;

public class GFG {
    public static void main(String[] args)
    {

        // create a LocalDate object
        LocalDate date
            = LocalDate.parse("2020-02-29");

        // print instance
        System.out.println("LocalDate before"
                           + " subtracting years: " + date);

        // subtract -21 years
        LocalDate returnvalue
            = date.minusYears(-21);

        // print result
        System.out.println("LocalDate after "
                           + " subtracting years: " + returnvalue);
    }
}
Output:
LocalDate before subtracting years: 2020-02-29
LocalDate after  subtracting years: 2041-02-28
Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#minusYears(long)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4