Skip to content

Commit 5561193

Browse files
committed
Better exception messages from FastDateParser.parse(String)
1 parent 2f3361d commit 5561193

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ The <action> type attribute can be add,update,fix,remove.
9898
<action type="fix" dev="ggregory" due-to="Gary Gregory, David Du">Simplify literal assignments in ArrayFillTest #1567.</action>
9999
<action type="fix" dev="ggregory" due-to="Gary Gregory, David Du">Make ArrayUtils methods implementation consistent with other overloads #1568.</action>
100100
<action type="fix" dev="ggregory" due-to="ThrawnCA, Gary Gregory">Fix handling of null marker in StringUtils.abbreviate(String, String, int, int) #1571.</action>
101+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Better exception messages from FastDateParser.parse(String).</action>
101102
<!-- ADD -->
102103
<action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion.JAVA_27.</action>
103104
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_27.</action>

src/main/java/org/apache/commons/lang3/time/FastDateParser.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.apache.commons.lang3.ArraySorter;
4848
import org.apache.commons.lang3.CharUtils;
4949
import org.apache.commons.lang3.LocaleUtils;
50+
import org.apache.commons.lang3.StringUtils;
5051

5152
/**
5253
* FastDateParser is a fast and thread-safe version of {@link java.text.SimpleDateFormat}.
@@ -323,7 +324,7 @@ boolean parse(final FastDateParser parser, final Calendar calendar, final String
323324
*/
324325
@Override
325326
public String toString() {
326-
return "NumberStrategy [field=" + field + "]";
327+
return getClass().getSimpleName() + " [field=" + field + "]";
327328
}
328329
}
329330

@@ -1027,11 +1028,12 @@ public Date parse(final String source) throws ParseException {
10271028
final Date date = parse(source, pp);
10281029
if (date == null) {
10291030
// Add a note regarding supported date range
1031+
final int errorIndex = pp.getErrorIndex();
1032+
final String msg = String.format("Unparseable date: '%s', parse position = %s", source, pp);
10301033
if (locale.equals(JAPANESE_IMPERIAL)) {
1031-
throw new ParseException("(The " + locale + " locale does not support dates before 1868 AD)\nUnparseable date: \"" + source,
1032-
pp.getErrorIndex());
1034+
throw new ParseException(String.format("; the %s locale does not support dates before 1868-01-01.", locale, msg), errorIndex);
10331035
}
1034-
throw new ParseException("Unparseable date: " + source, pp.getErrorIndex());
1036+
throw new ParseException(msg, errorIndex);
10351037
}
10361038
return date;
10371039
}
@@ -1129,6 +1131,6 @@ public String toString() {
11291131
*/
11301132
public String toStringAll() {
11311133
return "FastDateParser [pattern=" + pattern + ", timeZone=" + timeZone + ", locale=" + locale + ", century=" + century + ", startYear=" + startYear
1132-
+ ", patterns=" + patterns + "]";
1134+
+ ", patterns=" + StringUtils.join(patterns, ", " + System.lineSeparator() + "\t") + "]";
11331135
}
11341136
}

0 commit comments

Comments
 (0)