-3

Something strange happens when I am trying to convert date to a milliseconds. Maybe someone can explain my this:

 Calendar calendar = Calendar.getInstance();
 calendar.set(2017, 9, 3, 4, 50);

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss",
     Locale.getDefault());
 Log.i("tag", formatter.format(calendar.getTime()));

and logcat logs my out:

I/tag: 2017.10.03 04:50:34

Why months are different ?

2 Answers2

2

Calendar month is zero-based (0-11) but when displayed it's in "human" version (1-12).

Neria Nachum
  • 1,519
  • 1
  • 20
  • 37
2

In the method calendar.set(), from the documentation the parameter month starts from 0.

month the value used to set the MONTH calendar field. * Month value is 0-based. e.g., 0 for January.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59