java get current year

java get current year

To get the current year in Java, you can use the Year class from the java.time package. This class represents a year, and provides methods for working with years.

Here's an example of how to use the Year class to get the current year:

import java.time.Year;

public class Main {
  public static void main(String[] args) {
    Year currentYear = Year.now();
    int yearValue = currentYear.getValue();
    System.out.println(yearValue); // prints the current year
  }
}
‮ecruoS‬:www.lautturi.com

In this example, the now() method of the Year class is used to get the current year as a Year object. The getValue() method is then called on the Year object to get the year as an integer. The year is then printed to the console.

You can use the various methods of the Year class to manipulate the year, such as isLeap() to check if the year is a leap year, or minusYears() to subtract years.

Created Time:2017-11-03 22:21:09  Author:lautturi