Get year from date in Java
The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this date object as it is understood in the local time zone.
Since JDK version 1.1, Calendar.get has taken its position as the preferred method (Calendar.YEAR)
Syntax:
public int getYear()
Parameters
None
Return
The year indicated by this date is returned by the getYear() method.
GetYear.java
import java.util.Date;
public class GetYear {
public static void main(String[] args) {
Date d=new Date();
int year=d.getYear();
System.out.println("The year for the date object is : "+year);
System.out.println("***Add 1900 to the value of the year acquired from this date object to obtain the current year***");
int currentYear=year+1900;
System.out.println("The Current year is: "+currentYear);
}
}
Output:

How to Get the Current Year in Java Using Date and LocalDate Class
Java.util.Date the value returned by the getyear() function is deducted from 1900. However, Java long ago deprecated this approach. To do any Date and Time operations, we may instead utilize the LocalDate class included in the Java.time package.
Using the date.toInstant() method, which returns the time instant, we convert the date object to a LocalDate object. The system's default timezone is then specified by using atZone() after supplying the value of ZoneId.systemDefault() to the function Object() { [native code] } of atZone. Finally, we use the toLocalDate() function to change the instant value into a LocalDate.
Using the LocalDate object getLocalDate that we have now, we can obtain the year using getLocalDate.getYear().
GetYearProgram.java
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class GetYearProgram {
public static void main(String[] args) {
Date date = new Date();
ZoneId timeZone = ZoneId.systemDefault();
LocalDate getLocalDate = date.toInstant().atZone(timeZone).toLocalDate();
System.out.println(getLocalDate.getYear());
}
}
Output:

Using Date and SimpleDataFormat to determine the current year
When the time is set to 00:00:00, Date returns the time and date. We use SimpleDateFormat to prepare the date to obtain the year. By passing in the format yyyy, we generate a SimpleDateFormat object. The date is sent as an argument when format() is used. getYearFormat.format(date) gives back the outcome as a string, as shown in the output.
JavaGetYear.java
import java.text.SimpleDateFormat;
import java.util.Date;
public class JavaGetYear {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat getYearFormat = new SimpleDateFormat("yyyy");
String currentYear = getYearFormat.format(date);
System.out.println(currentYear);
}
}
Output:

How to get the Current Year Using Date.getYear() and Calendar class
The final method uses the Calendar class, which is supposed to contain several Java.util.Date methods. We construct a Date object and obtain a calendar instance using calendar. getInstance().Then we call the function calendar.setTime() that sets the time on the Calendar using the date.
To obtain the year, we now call a calendar.get() function that returns the value of the field supplied as its argument. The Calendaer.YEAR year is provided to us as an integer.
DateGetYearJava.java
import java.util.Calendar;
import java.util.Date;
public class DateGetYearJava {
public static void main(String[] args) {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dateYear = calendar.get(Calendar.YEAR);
System.out.println(dateYear);
}
}
Output:

java.time.LocalDate.getYear() Method Example
Explanation
The Java.time.LocalDate.getYear() approach obtains the year field.
Declaration
The Java.time.LocalDate.getYear() method declaration is as follows.
public int getYear()
Return Value
from MIN YEAR to MAX YEAR, the year
GetYearInJava.java
import java.time.LocalDate;
public class GetYearInJava {
public static void main(String[] args) {
LocalDate date = LocalDate.parse("2022-02-03");
System.out.println(date.getYear());
}
}
Output

GetYear.java
// Java Program to Get Year From Date
// Using the LocalDate class
// Introducing Classes/Files
import java.time.LocalDate;
import java.time.Month;
import java.util.Date;
// Primary class
class GetYear {
// Method 1
// To get the year
public static void getYear(String date)
{
// Obtaining a LocalTime instance from a date
LocalDate currentDate = LocalDate.parse(date);
// Getting the year from the date
int year = currentDate.getYear();
// The year is printed.
System.out.println("Year: " + year);
}
// Method 2
// The primary driver method
public static void main(String args[])
{
// Setting a date
String date = "2022-10-21";
// Special Call
getYear(date);
}
}
Output:

GetYearInJava.java
// Java Program to Get Year From Date
// using the calendar class
// Classes Importing/Files
import java.util.*;
// main class
class GetYearInJava {
// Driver Main Code
public static void main(String args[])
{
// Developing an object for a calendar
Calendar Cal
= new GregorianCalendar(
2022, 10, 21);
// Developing an object for a calendar
// object
int year = Cal.get(Calendar.YEAR);
// The year value is being printed
System.out.println("Year: " + year);
}
}
Output:
