Getting two, three, four Previous Month and Year from Current Month and Year. Here is the Code of Getting Previous Month and Year
This Code return You Month and Year as String With Your Specific Format
This Code return You Month and Year as int
Calendar current=Calendar.getInstance();
int currentmonth = current.get(Calendar.MONTH) + 1;
int currenyr = current.get(Calendar.YEAR);
Calendar previous = Calendar.getInstance();
//fetching previous Two month Date
previous.add(Calendar.MONTH, -2);
int previousmonth = previous.get(Calendar.MONTH) + 1;
// beware of month indexing from zero
int previousyr = previous.get(Calendar.YEAR);
System.out.println("Currentmonth and Year:" + currentmonth + "....." + currenyr);
System.out.println("Previousmonth and Year:" + previousmonth + ".." + previousyr);
output: Current month and Year:1.....2019
output: Previous month and Year:11.....2019
int currentmonth = current.get(Calendar.MONTH) + 1;
int currenyr = current.get(Calendar.YEAR);
Calendar previous = Calendar.getInstance();
//fetching previous Two month Date
previous.add(Calendar.MONTH, -2);
int previousmonth = previous.get(Calendar.MONTH) + 1;
// beware of month indexing from zero
int previousyr = previous.get(Calendar.YEAR);
System.out.println("Currentmonth and Year:" + currentmonth + "....." + currenyr);
System.out.println("Previousmonth and Year:" + previousmonth + ".." + previousyr);
output: Current month and Year:1.....2019
output: Previous month and Year:11.....2019
This Code return You Month and Year as String With Your Specific Format
Calendar c = new GregorianCalendar();
//if you Want to get a static Year, month and Date you can use this Line
//c.set(2018, Calendar.JANUARY, 01);
//this Line is Used for Current Year and Month
c.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("MMM");
SimpleDateFormat sd2 = new SimpleDateFormat("YYYY");
System.out.println(sdf.format(c.getTime())+"..."+sd2.format(c.getTime()));// NOW
c.add(Calendar.MONTH, -1);
System.out.println(sdf.format(c.getTime())+"..."+sd2.format(c.getTime())); // One month ago
c.add(Calendar.MONTH, -1);
System.out.println(sdf.format(c.getTime())+"..."+sd2.format(c.getTime()));// Two month ago
output:Jan...2019
Dec...2018
Nov...2018
//if you Want to get a static Year, month and Date you can use this Line
//c.set(2018, Calendar.JANUARY, 01);
//this Line is Used for Current Year and Month
c.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("MMM");
SimpleDateFormat sd2 = new SimpleDateFormat("YYYY");
System.out.println(sdf.format(c.getTime())+"..."+sd2.format(c.getTime()));// NOW
c.add(Calendar.MONTH, -1);
System.out.println(sdf.format(c.getTime())+"..."+sd2.format(c.getTime())); // One month ago
c.add(Calendar.MONTH, -1);
System.out.println(sdf.format(c.getTime())+"..."+sd2.format(c.getTime()));// Two month ago
output:Jan...2019
Dec...2018
Nov...2018