public Person read() throws IOException { Person person = null; if (fileScanner.hasNext()) { String s = fileScanner.nextLine(); String[] parts = s.split(" "); String firstName = parts[1]; String middleName = parts[2]; String lastName = parts[0]; Calendar calendar = new GregorianCalendar(Integer.parseInt(parts[5]), Integer.parseInt(parts[4])-1, Integer.parseInt(parts[3])); person = new Person(firstName, middleName, lastName, calendar.getTime()); } return person; } 

This is the work with the month, Calendar calendar = new GregorianCalendar , for which the -1 just do not understand

  • usually -1 used to decrease the number by one. What kind of logic the author laid in this code, I don’t know, but it would be logical to assume that the value of parts[4] goes beyond certain limits! - JVic
  • one
    If any answer answers you are satisfied, then do not forget to accept it (check mark to the left of the answer). - Regent

2 answers 2

In this case, the constructor is called:

GregorianCalendar (int year, int month, int dayOfMonth)

Which is the second parameter takes the value of the month. Moreover, the documentation clearly states that the months are counted from 0 (that is, January is 0 and December is 11).

Therefore, if the value of the month with fileScanner is read as 1-12, then to reduce it to the format 0-11, subtract one from it.

    To month was from 0 to 11, and not from 1 to 12.