xxxxxxxxxx
import java.util.Calendar;
int year = Calendar.getInstance().get(Calendar.YEAR);
To get the current year in Java, you can use the java.time.Year class from the Java 8 Date-Time API or the java.util.Calendar class from earlier Java versions. Here are examples of both approaches:
Using java.time.Year (Java 8 and later):
xxxxxxxxxx
import java.time.Year;
public class MyClass {
public static void main(String[] args) {
int yearValue = Year.now().getValue();
System.out.println("Current Year: " + yearValue);
}
}
xxxxxxxxxx
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(datetime);
SimpleDateFormat df = new SimpleDateFormat("yyyy");
year = df.format(date);
xxxxxxxxxx
import java.time.ZoneId;
import java.util.Date;
new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().getYear();