Calendar.MONTH is giving an invalid value

I am trying to get the actual month from Calendar using the following:

 Calendar c = Calendar.getInstance(); String time = String.valueOf(c.get(Calendar.MONTH)); 

In accordance with the system settings "Settings β†’ Date and time" the actual month is 10, and get(Calendar.MONTH) is 09.

+6
source share
5 answers

Keep in mind that month values ​​start at 0 , so October is actually month number 9 .

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#MONTH

+19
source

Calendar.MONTH returns a month that is zero, so it gives 1 less than the actual month Add 1 to get the correct value

 String time = String.valueOf(c.get(Calendar.MONTH)+1); 
+6
source

Calendar.MONTH

returns

 0 for 1st month (jan) 1 for 2nd month (feb) . . 11 for 12th month (dec) 

Documentation

So change your code to

 String time = String.valueOf(c.get(Calendar.MONTH)+1);// added 1 
+2
source

Calendar.MONTH value starts from 0 to 11, not from 1 to 12.

You can check the value of Calendar.JANUARY = 0.

Contact: http://developer.android.com/reference/java/util/Calendar.html#JANUARY

0
source

I suggest to try

 @Override public void onSelectedDayChange(CalendarView view, int year, int month, int day) { Toast.makeText(getApplicationContext(), month+1 + "/" + day + "/" + year, Toast.LENGTH_SHORT).show(); calDate = month+1 + "/" + day + "/" + year; } }); 
0
source

Source: https://habr.com/ru/post/955876/


All Articles