Run the test case below. According to the javadoc, years should be formatted according to the rule for "Number" (that is, the number of format characters specifies the minimum number of digits, with zero-padding), except for 'yy', where the year is truncated to two digits. The implementation produces different results. Below are expected and actual results for the year 2000:
Format string Expected result Actual result
------------- --------------- -------------
y 2000 00
yy 00 00
yyy 2000 00
yyyy 2000 2000
yyyyy 02000 2000
import java.text.SimpleDateFormat;
import java.util.Date;
public class YearFormatTest {
public static void main(String args[]) {
SimpleDateFormat sdf = new SimpleDateFormat();
Date today = new Date();
sdf.applyPattern("MM d y");
System.out.println(sdf.format(today));
sdf.applyPattern("MM d yy");
System.out.println(sdf.format(today));
sdf.applyPattern("MM d yyy");
System.out.println(sdf.format(today));
sdf.applyPattern("MM d yyyy");
System.out.println(sdf.format(today));
sdf.applyPattern("MM d yyyyy");
System.out.println(sdf.format(today));
}
}
Format string Expected result Actual result
------------- --------------- -------------
y 2000 00
yy 00 00
yyy 2000 00
yyyy 2000 2000
yyyyy 02000 2000
import java.text.SimpleDateFormat;
import java.util.Date;
public class YearFormatTest {
public static void main(String args[]) {
SimpleDateFormat sdf = new SimpleDateFormat();
Date today = new Date();
sdf.applyPattern("MM d y");
System.out.println(sdf.format(today));
sdf.applyPattern("MM d yy");
System.out.println(sdf.format(today));
sdf.applyPattern("MM d yyy");
System.out.println(sdf.format(today));
sdf.applyPattern("MM d yyyy");
System.out.println(sdf.format(today));
sdf.applyPattern("MM d yyyyy");
System.out.println(sdf.format(today));
}
}
- relates to
-
JDK-6609750 [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
-
- Closed
-