fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.text.SimpleDateFormat;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10. import java.util.Locale;
  11.  
  12.  
  13. /* Name of the class has to be "Main" only if the class is public. */
  14. class Ideone
  15. {
  16. public static void main (String[] args) throws java.lang.Exception
  17. {
  18. System.out.println("Ultimo lunes de Enero 2018 :: " + getLastMondayofMonth(1, 2018));
  19. System.out.println("Ultimo lunes de Febrero 2018 :: " + getLastMondayofMonth(2, 2018));
  20. System.out.println("Ultimo lunes de Marzo 2018 :: " + getLastMondayofMonth(3, 2018));
  21. System.out.println("Ultimo lunes de Abril 2018 :: " + getLastMondayofMonth(4, 2018));
  22. System.out.println("Ultimo lunes de Mayo 2018 :: " + getLastMondayofMonth(5, 2018));
  23. System.out.println("Ultimo lunes de Junio 2018 :: " + getLastMondayofMonth(6, 2018));
  24. System.out.println("Ultimo lunes de Julio 2018 :: " + getLastMondayofMonth(7, 2018));
  25. }
  26.  
  27. private static String getLastMondayofMonth(int month, int year){
  28. final String DATE_FORMAT_NOW = "dd-MMM-yyyy";
  29. Calendar cal = Calendar.getInstance();
  30. SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  31.  
  32. int dayofweek;
  33. cal.set(Calendar.MONTH, month - 1 /* January 0 and so on..*/);
  34. cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  35. cal.set(Calendar.YEAR, year);
  36. dayofweek =cal.get(Calendar.DAY_OF_WEEK);
  37. if (dayofweek < Calendar.MONDAY) {
  38. cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 7 + Calendar.MONDAY - dayofweek);
  39. }else {
  40. cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + Calendar.MONDAY - dayofweek);
  41. }
  42. return sdf.format(cal.getTime());
  43. }
  44.  
  45. }
Success #stdin #stdout 0.13s 29884KB
stdin
Standard input is empty
stdout
Ultimo lunes de Enero 2018 :: 29-Jan-2018
Ultimo lunes de Febrero 2018 :: 26-Feb-2018
Ultimo lunes de Marzo 2018 :: 26-Mar-2018
Ultimo lunes de Abril 2018 :: 30-Apr-2018
Ultimo lunes de Mayo 2018 :: 28-May-2018
Ultimo lunes de Junio 2018 :: 25-Jun-2018
Ultimo lunes de Julio 2018 :: 30-Jul-2018