java.util.Date.parse() does not parse dates of the form "2/4/96" correctly.
The Java Language Specification has a very detailed description of the Date
class' parse method in chapter 20, section 20.3.31. In its discussion of
how it treats numbers that it finds in its argument it says:
If the number is followed by a slash, it is regarded as a month (it is
decreased by 1 to produce a number in the range 0 to 11), unless a month
has
already been recognized, in which case it is regarded as a day of the month.
The following code reproduces the problem.
import java.util.Date;
public class DateParse {
public static void main(String argv[]) {
Date d = new Date("2/4/96");
System.out.println("getMonth() returns " + d.getMonth());
System.out.println(d);
}
}
Output:
java DateParse
getMonth() returns 2
Mon Mar 04 00:00:00 EST 1996
According to the spec parse() should have decremented the month by one
to provide 0 based enumeration. getMonth() should have returned 1 and the month Feb.
Correct output should have been:
java DateParse
getMonth() returns 1
Mon Feb 04 00:00:00 EST 1996
The Java Language Specification has a very detailed description of the Date
class' parse method in chapter 20, section 20.3.31. In its discussion of
how it treats numbers that it finds in its argument it says:
If the number is followed by a slash, it is regarded as a month (it is
decreased by 1 to produce a number in the range 0 to 11), unless a month
has
already been recognized, in which case it is regarded as a day of the month.
The following code reproduces the problem.
import java.util.Date;
public class DateParse {
public static void main(String argv[]) {
Date d = new Date("2/4/96");
System.out.println("getMonth() returns " + d.getMonth());
System.out.println(d);
}
}
Output:
java DateParse
getMonth() returns 2
Mon Mar 04 00:00:00 EST 1996
According to the spec parse() should have decremented the month by one
to provide 0 based enumeration. getMonth() should have returned 1 and the month Feb.
Correct output should have been:
java DateParse
getMonth() returns 1
Mon Feb 04 00:00:00 EST 1996
- duplicates
-
JDK-4013088 java.util.Date parses month incorrectly
- Closed