String Length, Indexes, and substring

Length and indexes
A calendar app stores a date as text.
String date = "2026-10-09";
The call date.length() returns 10, the number of characters, and the two hyphens count. Each character has an index, its position counted from 0.
character  2 0 2 6 - 1 0 - 0 9
index 0 1 2 3 4 5 6 7 8 9
Counting starts at 0, so the last of the 10 characters sits at index 9. The index 10 isn't a position in this string.
substring with two arguments
The call date.substring(0, 4) returns "2026". The characters run from index 0 up to index 4, and the character at index 4 isn't included. The second argument is the first index left out.
String month = date.substring(5, 7);
This gives "10", the characters at indexes 5 and 6. Subtracting the first argument from the second gives how many characters come back: 7 minus 5 is 2. The original string isn't changed, since substring returns a new String.
substring with one argument
With one argument, the result starts at that index and keeps every character after it.
String day = date.substring(8);
This gives "09". It's the same as date.substring(8, date.length()), which is date.substring(8, 10). The argument can equal the length: date.substring(10) returns the empty string "", since no characters are left after the last one.
One character
A single character is taken out with two arguments one apart. The separator after the year is at index 4.
String sep = date.substring(4, 5);
This gives "-", a String holding one character. In general substring(i, i + 1) returns the character at index i as a String.
Out of range
The call date.substring(8, 11) asks for characters through index 10, which doesn't exist. The code compiles, but when it runs Java throws a StringIndexOutOfBoundsException. A first argument larger than the second, as in date.substring(7, 5), throws the same exception.