public class CalendarCal {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat simpleDateformat = new SimpleDateFormat("yy");
String m = simpleDateformat.format(date);
System.out.println("Year:" + m);
int year = Calendar.getInstance().get(Calendar.YEAR) % 100;
System.err.println(year);
}
}
I am able to retrieve the current year from this. How can I get the last (previous) year with help of utility? I don't want to do a calculation like currentyear - 1
or like that.
time. LocalDate. Use LocalDate 's plusDays() and minusDays() method to get the next day and previous day, by adding and subtracting 1 from today.
The now() method of this class obtains the current date from the system clock. The getYear() method returns an integer representing the year filed in the current LocalDate object. The getMonth() method returns an object of the java. timeMonth class representing the month in the LocalDate object.
To compare dates if a date is after another date, use the Calendar. after() method.
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);
}
public static void main(String[] args) {
System.out.println(getPreviousYear());
}
Prints on my system:
2011
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With