fork(1) download
  1. import java.time.YearMonth;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.format.DateTimeFormatterBuilder;
  4. import java.util.Locale;
  5.  
  6. public class Main {
  7. public static void main(String[] args) {
  8. DateTimeFormatter dtf = new DateTimeFormatterBuilder()
  9. .parseCaseInsensitive()
  10. .appendPattern("MMM/uuuu")
  11. .toFormatter(Locale.ROOT);
  12.  
  13. String currentMonth = "SEP/2021";
  14.  
  15. YearMonth currentYm = YearMonth.parse(currentMonth, dtf);
  16. YearMonth previousYm = currentYm.minusMonths(1);
  17.  
  18. String previousMonth = previousYm.format(dtf);
  19. System.out.println(previousMonth);
  20.  
  21. // If required, convert it into the UPPER CASE
  22. System.out.println(previousMonth.toUpperCase());
  23. }
  24. }
Success #stdin #stdout 0.13s 52120KB
stdin
Standard input is empty
stdout
Aug/2021
AUG/2021