Search code examples
javaandroiddateandroid-calendarjava.util.calendar

Set Calendar with specific date and get wrong values for month


I am using this method to set time to the calendar but when I want to get month it'll give me month-1.

For example when I set "2017-12-27 10:50:00". And when I want to get the month of the calendar it's 11 instead of 12.

   public String setDate(String gDate){

       try {

           private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

           Date date = sdf.parse(gDate);
           javaCalendar.setTime(date);

       } catch (ParseException e) {
           e.printStackTrace();
           Log.i("LOG_DATE", "Error is: " + e.toString());
       }
       return "";
   } 

Solution

  • It's because The Months are numbered from 0 (January) to 11 (December)

    for more visit https://stackoverflow.com/a/344400/5602752