Reputation: 1196
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.
Upvotes: 10
Views: 44320
Reputation: 21
import java.time.Year;
public class TryTime {
public static void main(String[] args) {
System.out.println(Year.now().minusYears(1));
}
}
Upvotes: 2
Reputation: 1
Calendar calendarEnd=Calendar.getInstance();
// You can substract from the current Year to get the previous year last dates.
calendarEnd.set(Calendar.YEAR,calendarEnd.get(Calendar.YEAR)-3);
calendarEnd.set(Calendar.MONTH,11);
calendarEnd.set(Calendar.DAY_OF_MONTH,31);
Date endDate=calendarEnd.getTime();
System.out.println("\nEND Date Of this year : "+endDate);
Upvotes: 0
Reputation: 339342
Using Joda-Time 2.4:
int year = DateTime.now( DateTimeZone.forID( "Europe/Paris" ) ).minusYears( 1 ).getYear();
Upvotes: 0
Reputation: 2800
It is as simple as explained below:
//Create calendar instance
Calendar instance = Calendar.getInstance();
//Set your date to it
instance.setTime(date);
//Substract one year from it
instance.add(Calendar.YEAR, -1);
//Use the pattern you wish to display the date
SimpleDateFormat isoFormat = new SimpleDateFormat("dd-MMM-yyyy");
// Convert the date to string using format method
String previousYearDate = isoFormat.format(instance.getTime());
Upvotes: 1
Reputation: 3530
Light weight solution (without direct or indirect constructor call of any Object).
final class YearUtil {
static final int MILLIS_IN_SECOND = 1000;
static final int SECONDS_IN_MINUTE = 60;
static final int MINUTES_IN_HOUR = 60;
static final int HOURS_IN_DAY = 24;
static final int DAYS_IN_YEAR = 365;
static final long MILLISECONDS_IN_YEAR = (long) MILLIS_IN_SECOND
* SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY * DAYS_IN_YEAR;
public static int getCurrentYear() {
return (int) (1970 + System.currentTimeMillis() / MILLISECONDS_IN_YEAR);
}
}
Than use it:
int prevYear = YearUtil.getCurrentYear() - 1;
These numbers are so big that there is no need to bother about even years (in your live and your childs childs childs...) ;)
Upvotes: 0
Reputation: 43504
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
Upvotes: 26
Reputation: 31647
import java.util.*;
public class GetYear {
public static void main(String []arg){
Calendar cal=Calendar.getInstance();
int year=cal.get(Calendar.YEAR)-1;
System.out.println("Current Year: " + year );
}
}
Upvotes: 0
Reputation: 6403
public static String previousDateString(String dateString)
throws ParseException {
// Create a date formatter using your format string
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
// Parse the given date string into a Date object.
// Note: This can throw a ParseException.
Date myDate = dateFormat.parse(dateString);
// Use the Calendar class to subtract one day
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -1);
// Use the date formatter to produce a formatted date string
Date previousDate = calendar.getTime();
String result = dateFormat.format(previousDate);
return result;
}
Upvotes: 0