I am getting the current month as follows:
int mesActual = new Date().getMonth() +1; //enero=0 diciembre=11
But this method is obsolete. How do I replace it?
I am getting the current month as follows:
int mesActual = new Date().getMonth() +1; //enero=0 diciembre=11
But this method is obsolete. How do I replace it?
The answer of @Rene Limon is the solution, you could omit the date to write less lines of unnecessary code, thus:
Calendar cal = Calendar.getInstance();
int mesActual = cal.get(Calendar.MONTH) + 1;
Like this:
java.util.Date date= new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int mesActual = cal.get(Calendar.MONTH)+1;
You have to get the day, month or year from a Calendar instance:
//int mesActual = new Date().getMonth() +1;//enero=0 diciembre=11
Calendar calendar = Calendar.getInstance();
int diaActual = calendar.get(Calendar.DAY_OF_WEEK);
int mesActual = calendar.get(Calendar.MONTH) + 1;
int anioActual = calendar.get(Calendar.YEAR);
Important to note that to obtain the month you must add 1, since the first month is 0, for this reason you get this way:
int mesActual = calendar.get(Calendar.MONTH) + 1;
MONTH : Field number for get and set that indicates the month. This is a specific value of the calendar. The first month of the year in Gregorian and Julian calendars is JANUARY which is 0; The last It depends on the number of months in a year.