1

Why code return string value is 19880420 instead of 19880320?

SimpleDateFormat template= new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.set(1988, 3, 20, 18, 00);

return template.format(cal.getTime()); 
user1974307
  • 107
  • 2
  • 5

2 Answers2

2

Month beginns with 0 not with 1. :)

Have a look here: Why is January month 0 in Java Calendar?

Community
  • 1
  • 1
pL4Gu33
  • 2,045
  • 16
  • 38
1

Because January is month 0 so you have to do -1 when constructing your calendar http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#MONTH

Erwan C.
  • 709
  • 5
  • 18