-1

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 "";
   } 
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Ehsan
  • 2,676
  • 6
  • 29
  • 56
  • 2
    It starts with 0. it is 0 to 11. – Vidhi Dave Dec 27 '17 at 07:24
  • share you code to get calendar month – Apurva Kolapkar Dec 27 '17 at 07:27
  • 1
    Even on Android, consider dropping the old classes `Date`, `Calendar` and `SimpleDateFormat` and using `LocalDateTime` and `DateTimeFormatter` from JSR-310, the modern Java date and time API. It is so much nicer to work with. And will give you the 12th month as either `Month.DECEMBER` or 12, you choose. To use JSR-310 on Android, add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your project. – Ole V.V. Dec 27 '17 at 16:23

1 Answers1

2

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

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

Munir
  • 2,548
  • 1
  • 11
  • 20