50

How do I find out the last month and its year in Java?

e.g. If today is Oct. 10 2012, the result should be Month = 9 and Year = 2012. If today is Jan. 10 2013, the result should be Month = 12 and Year = 2012.

alex
  • 6,818
  • 9
  • 52
  • 103
ajm
  • 12,863
  • 58
  • 163
  • 234
  • 5
    Look at the API documentation of `java.util.Calendar`. Or make your life easier by using [Joda Time](http://joda-time.sourceforge.net/). – Jesper Oct 10 '12 at 07:52
  • 3
    If using java.util.Calendar be careful with the id's of months. January is zero not one. – Tinman Oct 10 '12 at 07:55
  • Refere this Example : http://www.java-examples.com/add-or-substract-months-current-date-using-java-calendar – Nirav Ranpara Oct 10 '12 at 07:56
  • 1
    Jesper and Tinman, I recommend you don’t use `Calendar`. That class is poorly designed and long outdated. Instead use `YearMonth` or `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 02 '19 at 06:35

10 Answers10

77

Your solution is here but instead of addition you need to use subtraction

c.add(Calendar.MONTH, -1);

Then you can call getter on the Calendar to acquire proper fields

int month = c.get(Calendar.MONTH) + 1; // beware of month indexing from zero
int year  = c.get(Calendar.YEAR);
Community
  • 1
  • 1
Gaim
  • 6,734
  • 4
  • 38
  • 58
36

java.time

Using java.time framework built into Java 8:

import java.time.LocalDate;

LocalDate now = LocalDate.now(); // 2015-11-24
LocalDate earlier = now.minusMonths(1); // 2015-10-24

earlier.getMonth(); // java.time.Month = OCTOBER
earlier.getMonth.getValue(); // 10
earlier.getYear(); // 2015
Bryan Roth
  • 10,479
  • 15
  • 47
  • 56
Przemek
  • 7,111
  • 3
  • 43
  • 52
12

Use Joda Time Library. It is very easy to handle date, time, calender and locale with it and it will be integrated to java in version 8.

DateTime#minusMonths method would help you get previous month.

DateTime month = new DateTime().minusMonths (1); 
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • **Update:** The *Joda-Time* project is now in maintenance-mode. Its creator Stephen Colebourne went on to lead its successor, the *java.time* classes defined in JSR 310. – Basil Bourque May 13 '21 at 06:44
10

you can use the Calendar class to do so:

SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
System.out.println(format.format(cal.getTime()));

This prints : 2012.09.10 11:01 for actual date 2012.10.10 11:01

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • **Update:** These terrible classes are now obsolete. Supplanted years ago by the modern *java.time* classes defined in JSR 310, built into Java 8 and later, and available for Java 6 & 7 in the *ThreeTen-Backport* project. – Basil Bourque May 13 '21 at 06:45
7

The simplest & least error prone approach is... Use Calendar's roll() method. Like this:

    c.roll(Calendar.MONTH, false);

the roll method takes a boolean, which basically means roll the month up(true) or down(false)?

Herupkhart
  • 499
  • 4
  • 23
5
private static String getPreviousMonthDate(Date date){
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

    Calendar cal = Calendar.getInstance();  
    cal.setTime(date);  
    cal.set(Calendar.DAY_OF_MONTH, 1);  
    cal.add(Calendar.DATE, -1);

    Date preMonthDate = cal.getTime();  
    return format.format(preMonthDate);
}


private static String getPreToPreMonthDate(Date date){
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

    Calendar cal = Calendar.getInstance();  
    cal.setTime(date);  
    cal.add(Calendar.MONTH, -1);  
    cal.set(Calendar.DAY_OF_MONTH,1);  
    cal.add(Calendar.DATE, -1);  

    Date preToPreMonthDate = cal.getTime();  
    return format.format(preToPreMonthDate);
}
prasad kamble
  • 81
  • 1
  • 3
5

YearMonth class

You can use the java.time.YearMonth class, and its minusMonths method.

YearMonth lastMonth = YearMonth.now().minusMonths(1);

Calling toString gives you output in standard ISO 8601 format: yyyy-mm

You can access the parts, the year and the month. You may choose to use the Month enum object, or a mere int value 1-12 for the month.

int year = lastMonth.getYear() ;
int month = lastMonth.getMonthValue() ;
Month monthEnum = lastMonth.getMonth() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Prashant Shubham
  • 456
  • 8
  • 20
1

You need to be aware that month is zero based so when you do the getMonth you will need to add 1. In the example below we have to add 1 to Januaray as 1 and not 0

    Calendar c = Calendar.getInstance();
    c.set(2011, 2, 1);
    c.add(Calendar.MONTH, -1);
    int month = c.get(Calendar.MONTH) + 1;
    assertEquals(1, month);
RNJ
  • 15,272
  • 18
  • 86
  • 131
0

You get by using the LocalDate class.

For Example:

  1. To get last month date:

    LocalDate.now().minusMonths(1);

  2. To get starting date of last month

    LocalDate.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());

Similarly for Year:

  1. To get last year date:

    LocalDate.now().minusYears(1);

  2. To get starting date of last year :

    LocalDate.now().minusYears(1).with(TemporalAdjusters.lastDayOfYear());

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
Kamil007
  • 21
  • 6
-2

Here's the code snippet.I think it works.

      Calendar cal = Calendar.getInstance();
      SimpleDateFormat simpleMonth=new SimpleDateFormat("MMMM YYYY");
      cal.add(Calendar.MONTH, -1);
      System.out.println(simpleMonth.format(prevcal.getTime()));
Anzz
  • 11
  • 4