diff --git a/src/main/java/com/ethlo/time/DateTime.java b/src/main/java/com/ethlo/time/DateTime.java index 3be8072..c62c03c 100644 --- a/src/main/java/com/ethlo/time/DateTime.java +++ b/src/main/java/com/ethlo/time/DateTime.java @@ -375,7 +375,7 @@ private String toString(final DateTime date, final Field lastIncluded, final int { if (lastIncluded.ordinal() > date.getMostGranularField().ordinal()) { - throw new DateTimeFormatException("Requested granularity was " + lastIncluded.name() + ", but contains only granularity " + date.getMostGranularField().name()); + throw new DateTimeFormatException(String.format("Requested granularity was %s, but contains only granularity %s", lastIncluded.name(), date.getMostGranularField().name())); } final TimezoneOffset tz = date.getOffset().orElse(null); final char[] buffer = new char[35]; @@ -580,7 +580,7 @@ private void validated() if (second > 59) { - throw new DateTimeException("Invalid value for SecondOfMinute (valid values 0 - 59): " + second); + throw new DateTimeException(String.format("Invalid value for SecondOfMinute (valid values 0 - 59): %d", second)); } } diff --git a/src/main/java/com/ethlo/time/internal/ErrorUtil.java b/src/main/java/com/ethlo/time/internal/ErrorUtil.java index 806218f..036138b 100644 --- a/src/main/java/com/ethlo/time/internal/ErrorUtil.java +++ b/src/main/java/com/ethlo/time/internal/ErrorUtil.java @@ -34,17 +34,17 @@ private ErrorUtil() public static DateTimeParseException raiseUnexpectedCharacter(String chars, int index) { - throw new DateTimeParseException("Unexpected character " + chars.charAt(index) + " at position " + (index + 1) + ": " + chars, chars, index); + throw new DateTimeParseException(String.format("Unexpected character %s at position %d: %s", chars.charAt(index), index + 1, chars), chars, index); } public static DateTimeParseException raiseUnexpectedEndOfText(final String chars, final int offset) { - throw new DateTimeParseException("Unexpected end of input: " + chars, chars, offset); + throw new DateTimeParseException(String.format("Unexpected end of input: %s", chars), chars, offset); } public static DateTimeParseException raiseMissingGranularity(Field field, final String chars, final int offset) { - throw new DateTimeParseException("Unexpected end of input, missing field " + field.name() + ": " + chars, chars, offset); + throw new DateTimeParseException(String.format("Unexpected end of input, missing field %s: %s", field.name(), chars), chars, offset); } public static void assertPositionContains(Field field, String chars, int index, char expected) @@ -56,7 +56,7 @@ public static void assertPositionContains(Field field, String chars, int index, if (chars.charAt(index) != expected) { - throw new DateTimeParseException("Expected character " + expected + " at position " + (index + 1) + ", found " + chars.charAt(index) + ": " + chars, chars, index); + throw new DateTimeParseException(String.format("Expected character %s at position %d, found %s: %s", expected, index + 1, chars.charAt(index), chars), chars, index); } } @@ -64,12 +64,12 @@ public static void assertFractionDigits(String chars, int fractionDigits, int id { if (fractionDigits == 0) { - throw new DateTimeParseException("Must have at least 1 fraction digit: " + chars, chars, idx); + throw new DateTimeParseException(String.format("Must have at least 1 fraction digit: %s", chars), chars, idx); } if (fractionDigits > MAX_FRACTION_DIGITS) { - throw new DateTimeParseException("Maximum supported number of fraction digits in second is 9, got " + fractionDigits + ": " + chars, chars, idx); + throw new DateTimeParseException(String.format("Maximum supported number of fraction digits in second is 9, got %d: %s", fractionDigits, chars), chars, idx); } } } diff --git a/src/main/java/com/ethlo/time/internal/ITUParser.java b/src/main/java/com/ethlo/time/internal/ITUParser.java index 719cda7..d2bceb1 100644 --- a/src/main/java/com/ethlo/time/internal/ITUParser.java +++ b/src/main/java/com/ethlo/time/internal/ITUParser.java @@ -84,7 +84,8 @@ private static void assertAllowedDateTimeSeparator(final int offset, final Strin final char needle = chars.charAt(index); if (!config.isDateTimeSeparator(needle)) { - throw new DateTimeParseException("Expected character " + (config.getDateTimeSeparators().length > 1 ? Arrays.toString(config.getDateTimeSeparators()) : config.getDateTimeSeparators()[0]) + " at position " + (index + 1) + ", found " + chars.charAt(index) + ": " + chars, chars, index); + final String allowedCharStr = config.getDateTimeSeparators().length > 1 ? Arrays.toString(config.getDateTimeSeparators()) : Character.toString(config.getDateTimeSeparators()[0]); + throw new DateTimeParseException(String.format("Expected character %s at position %d, found %s: %s", allowedCharStr, index + 1, chars.charAt(index), chars), chars, index); } } @@ -111,7 +112,7 @@ private static TimezoneOffset parseTimezone(int offset, final ParseConfig parseC if (left < 6) { - throw new DateTimeParseException("Invalid timezone offset: " + chars, chars, idx); + throw new DateTimeParseException(String.format("Invalid timezone offset: %s", chars), chars, idx); } int hours = parsePositiveInt(chars, idx + 1, idx + 3); @@ -137,7 +138,7 @@ private static void assertNoMoreChars(final int offset, final ParseConfig parseC { if (chars.length() > lastUsed + 1) { - throw new DateTimeParseException("Trailing junk data after position " + (lastUsed + 2) + ": " + chars, chars, lastUsed + 1); + throw new DateTimeParseException(String.format("Trailing junk data after position %d: %s", lastUsed + 2, chars), chars, lastUsed + 1); } } } @@ -153,12 +154,12 @@ public static DateTime parseLenient(final String chars, final ParseConfig parseC if (availableLength < 0) { - throw new IndexOutOfBoundsException("offset is " + offset + " which is equal to or larger than the input length of " + chars.length()); + throw new IndexOutOfBoundsException(String.format("offset is %d which is equal to or larger than the input length of %d", offset, chars.length())); } if (offset < 0) { - throw new IndexOutOfBoundsException("offset cannot be negative, was " + offset); + throw new IndexOutOfBoundsException(String.format("offset cannot be negative, was %d", offset)); } // Date portion @@ -289,6 +290,6 @@ public static OffsetDateTime parseDateTime(final String chars, int offset) } final Field field = dateTime.getMostGranularField(); final Field nextGranularity = Field.values()[field.ordinal() + 1]; - throw new DateTimeParseException("Unexpected end of input, missing field " + nextGranularity + ": " + chars, chars, field.getRequiredLength()); + throw new DateTimeParseException(String.format("Unexpected end of input, missing field %s: %s", nextGranularity, chars), chars, field.getRequiredLength()); } } \ No newline at end of file