How to get previous year from the result of current year in java?
Just use Calendar.add
method to "add" -1
to the year and wrap it in your own utility method:
private static int getPreviousYear() {
Calendar prevYear = Calendar.getInstance();
prevYear.add(Calendar.YEAR, -1);
return prevYear.get(Calendar.YEAR);
}
Example:
public static void main(String[] args) {
System.out.println(getPreviousYear());
}
Prints on my system:
2011