'getMonth ()' is deprecated

3

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?

    
asked by Maguz 15.12.2016 в 18:57
source

3 answers

5

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;
    
answered by 15.12.2016 / 19:12
source
2

Like this:

java.util.Date date= new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int mesActual = cal.get(Calendar.MONTH)+1;
    
answered by 15.12.2016 в 19:01
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.

    
answered by 15.12.2016 в 19:28