I am trying to get previous 12 month into an arraylist, from the given month(taken from DB).
List<String> allDates = new ArrayList<String>();
sqlQuery="select max(date) from Table_Name";
maxDate="Jan-2016"; (Result from Query);
To get previous 12 months from maxDate,where i use SimpleDateFormat.
I want to calculate the previous 12 months from Given Month (maxDate), not from current month, i tried the following code.
// Parsing maxDate to an integer (say Jan-2016 = 0, Feb-2016= 1)
Date date = new SimpleDateFormat("MMM-yyyy").parse(maxDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month=cal.get(Calendar.MONTH);
System.out.println("month : "+month);
// Looping to get previous 12 months from current month.
SimpleDateFormat month_date = new SimpleDateFormat("MMM-yyyy");
for (int i = 12; i > 0; i--) {
Calendar calendar1 = Calendar.getInstance();
calendar1.add(Calendar.MONTH, -i);
String month_name1 = month_date.format(calendar1.getTime());
allDates.add(month_name1);
}
System.out.println(allDates);
Since months are numbered from (0 - 11) i couldn't achieve it. Please suggest an idea to calculate previous 12 months from given month. Appreciate your help!