-3

I need a function that will return three strings:

  1. first string will contain the current month and the current year.
  2. second string will contain the previous month and current year.
  3. third string will contain two months ago and current year.

This, of course, should also work if current month is January, for example.

So right now, the results should be:

  • September 2015
  • August 2015
  • July 2015
Phylogenesis
  • 7,775
  • 19
  • 27
Daniel Stoica
  • 65
  • 2
  • 2
  • 9

5 Answers5

17

A Java 8 version (using the java.time.YearMonth class) is here.

YearMonth thisMonth    = YearMonth.now();
YearMonth lastMonth    = thisMonth.minusMonths(1);
YearMonth twoMonthsAgo = thisMonth.minusMonths(2);

DateTimeFormatter monthYearFormatter = DateTimeFormatter.ofPattern("MMMM yyyy");

System.out.printf("Today: %s\n", thisMonth.format(monthYearFormatter));
System.out.printf("Last Month: %s\n", lastMonth.format(monthYearFormatter));
System.out.printf("Two Months Ago: %s\n", twoMonthsAgo.format(monthYearFormatter));

This prints the following:

Today: September 2015

Last Month: August 2015

Two Months Ago: July 2015

Phylogenesis
  • 7,775
  • 19
  • 27
10
Calendar c = new GregorianCalendar();
c.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("MMMM YYYY");
System.out.println(sdf.format(c.getTime()));   // NOW
c.add(Calendar.MONTH, -1);
System.out.println(sdf.format(c.getTime()));   // One month ago
c.add(Calendar.MONTH, -1);
System.out.println(sdf.format(c.getTime()));   // Two month ago
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

Below is an example using LocalDate written in Kotlin.

import java.time.LocalDate
import java.time.format.TextStyle
import java.util.Locale

val currentMonth = LocalDate.now().month.getDisplayName(TextStyle.FULL, Locale.getDefault()) //February
val prevMonth = LocalDate.now().minusMonths(1).getDisplayName(TextStyle.FULL, Locale.getDefault()) //January
val twoMonthsAgo = LocalDate.now().minusMonths(2) //DECEMBER
Anne
  • 259
  • 3
  • 4
-1
  1. Get the Months you need (Current, Current -1 and Current -2) see here
  2. Get the Textual form of the Months like shown here
Community
  • 1
  • 1
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
-1
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public static void main(String[] args) {


Date currentDate = null;
String dateString = null;
try {
    Calendar c = new GregorianCalendar();
    c.set(Calendar.HOUR_OF_DAY, 0); // anything 0 - 23
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    //c.add(Calendar.MONTH, -1);//previous month
    //c.add(Calendar.MONTH, -2);//two months back
    currentDate = c.getTime(); // the midnight, that's the first second
    // of the day.


    SimpleDateFormat sdfr = new SimpleDateFormat("MMMM yyyy");
    dateString = sdfr.format(currentDate);
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println(dateString); //prints current date

}

Pankaj Saboo
  • 1,125
  • 1
  • 8
  • 27