Hello! I need to get the current date and time. Everything is displayed correctly, except for the month: it shows the 11th month.

java.util.Calendar c = java.util.Calendar.getInstance(); String sDate = c.get(Calendar.YEAR) + "." + c.get(Calendar.MONTH) + "." + c.get(Calendar.DAY_OF_MONTH) + " в " + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE); tv.setText(sDate); 
  • one
    Months in the calendar starts from 0. - V. Makhnutin

1 answer 1

Because January starts at 0, respectively, December is the 11th month. If you want to get exactly 12 instead of 11, then you can write like this)

 c.get(Calendar.MONTH)+1; 

Here is another option, only instead of 12 it returns the abbreviated version of the month "December",

 SimpleDateFormat sdfMM = new SimpleDateFormat("MMM"); sdfMM.format(c.get(Calendar.MONTH); 

And to get "December"

 SimpleDateFormat sdfMMM = new SimpleDateFormat("MMMM"); sdfMMM.format(c.get(Calendar.MONTH);