Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatePicker shows wrong value of month

I have a problem in DatePicker in android when I use getMonth() method then it will return a wrong value.

For example:

DatePicker datepicker=new DatePicker();  int day=date.getDayOfMonth(); int month=date.getMonth(); int year=date.getYear();  t.setText(""+day+" / "+month+" / "+year); 

If I will select aug 06 1987 then it will return 6/7/1987

I think it is an error, if not tell me the reason please.

like image 779
Alok Kumar Avatar asked Dec 17 '10 04:12

Alok Kumar


1 Answers

As described in the Android SDK, months are indexed starting at 0. This means August is month 8, or index 7, thus giving you the correct result.

It is a simple matter of adding 1 to the index returned by the API to get the traditional one-indexed month.

Although this behavior may seem strange, it is consistent with the java.util.Calendar class (although it is not consistent with joda.time.DateTime).

like image 77
PearsonArtPhoto Avatar answered Sep 30 '22 04:09

PearsonArtPhoto