15

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

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

According the system settings "Settings --> Date & Time" actual month is 10 while get(Calendar.MONTH) returns 09.

XorOrNor
  • 8,868
  • 12
  • 48
  • 81

6 Answers6

37

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

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

ssantos
  • 16,001
  • 7
  • 50
  • 70
9

Calendar.MONTH returns month which is zero based that is why it is giving 1 less than actual month Add 1 to get correct value

String time = String.valueOf(c.get(Calendar.MONTH)+1);
Manishika
  • 5,478
  • 2
  • 22
  • 28
4

Calendar.MONTH

returns

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

Docs

So change your code to

String time = String.valueOf(c.get(Calendar.MONTH)+1);// added 1 
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
0

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

You may check the value of Calendar.JANUARY is 0 not 1.

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

Prakash Bhagat
  • 1,406
  • 16
  • 30
0

I suggest trying

@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; }
});
thesecretmaster
  • 1,950
  • 1
  • 27
  • 39
  • Concatenating strings to make dates is unsafe for internationalization and a terrible practice. Please use the system library date formatters. – Robin Daugherty Jun 06 '16 at 18:53
0

use this

    public void DateDialog(){

    DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            int Month;
            Month=monthOfYear+1;
            fromDate.setText(year + "-" + Month + "-" + dayOfMonth);
        }};
    DatePickerDialog dpDialog=new DatePickerDialog(getActivity(), listener, year, month, day);
    dpDialog.getDatePicker().setMinDate(mcalendar.getTimeInMillis());
    dpDialog.show();
}
Karan Chunara
  • 518
  • 4
  • 15