Skip to content

Instantly share code, notes, and snippets.

@gorrotowi
Created November 29, 2016 18:27
Show Gist options
  • Save gorrotowi/d243f41fa083a464f3acd51374a590ae to your computer and use it in GitHub Desktop.
Save gorrotowi/d243f41fa083a464f3acd51374a590ae to your computer and use it in GitHub Desktop.
Methods to get current and past Months and name of them
public static int getCurrentYear() {
return Calendar.getInstance().get(Calendar.YEAR);
}
public static int[] getMonthsPos() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 0);
int month1 = new Timestamp(calendar.getTime().getTime()).getMonth();
calendar.add(Calendar.MONTH, -1);
int month2 = new Timestamp(calendar.getTime().getTime()).getMonth();
calendar.add(Calendar.MONTH, -1);
int month3 = new Timestamp(calendar.getTime().getTime()).getMonth();
calendar.add(Calendar.MONTH, -1);
int month4 = new Timestamp(calendar.getTime().getTime()).getMonth();
return new int[]{month4, month3, month2, month1};
}
public static String[] getMonths() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 0);
int month1 = new Timestamp(calendar.getTime().getTime()).getMonth();
calendar.add(Calendar.MONTH, -1);
int month2 = new Timestamp(calendar.getTime().getTime()).getMonth();
calendar.add(Calendar.MONTH, -1);
int month3 = new Timestamp(calendar.getTime().getTime()).getMonth();
calendar.add(Calendar.MONTH, -1);
int month4 = new Timestamp(calendar.getTime().getTime()).getMonth();
return new String[]{getMonthForInt(month4), getMonthForInt(month3), getMonthForInt(month2), getMonthForInt(month1), "Todos"};
}
public static String getMonthForInt(int num) {
String[] monthnames = {"Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"};
return monthnames[num];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment