Adding unit test for #39410. Implementing fix by switching from using Math.log to Integer.toString to figure out the number of digits.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@398530 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2006-05-01 05:32:43 +00:00
parent 2f4408d33c
commit 79c3898c90
2 changed files with 15 additions and 4 deletions

View File

@ -31,6 +31,8 @@
import java.util.Map;
import java.util.TimeZone;
import org.apache.commons.lang.Validate;
/**
* <p>FastDateFormat is a fast and thread-safe version of
* {@link java.text.SimpleDateFormat}.</p>
@ -93,9 +95,6 @@ public class FastDateFormat extends Format {
*/
public static final int SHORT = DateFormat.SHORT;
// package scoped as used by inner class
static final double LOG_10 = Math.log(10);
private static String cDefaultPattern;
private static Map cInstanceCache = new HashMap(7);
@ -1283,7 +1282,8 @@ public final void appendTo(StringBuffer buffer, int value) {
if (value < 1000) {
digits = 3;
} else {
digits = (int)(Math.log(value) / LOG_10) + 1;
Validate.isTrue(value > -1, "Negative values should not be possible", value);
digits = Integer.toString(value).length();
}
for (int i = mSize; --i >= digits; ) {
buffer.append('0');

View File

@ -250,6 +250,17 @@ public void testLowYearPadding() {
cal.set(999,0,1);
assertEquals("0999/01/01", format.format(cal));
}
/**
* Show Bug #39410 is solved
*/
public void testMilleniumBug() {
Calendar cal = Calendar.getInstance();
FastDateFormat format = FastDateFormat.getInstance("dd.MM.yyyy");
cal.set(1000,0,1);
assertEquals("01.01.1000", format.format(cal));
}
/**
* testLowYearPadding showed that the date was buggy
* This test confirms it, getting 366 back as a date