Skip to content

Commit 32a65ef

Browse files
author
duke
committed
Backport fe0ccdf5f8a5559a608d2e2cd2b6aecbe245c5ec
1 parent 01eb688 commit 32a65ef

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/java.base/share/classes/java/time/format/DateTimeFormatter.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,29 +2296,23 @@ public Object parseObject(String text, ParsePosition pos) {
22962296
DateTimeParseContext context;
22972297
try {
22982298
context = formatter.parseUnresolved0(text, pos);
2299-
} catch (IndexOutOfBoundsException ex) {
2300-
if (pos.getErrorIndex() < 0) {
2301-
pos.setErrorIndex(0);
2302-
}
2303-
return null;
2304-
}
2305-
if (context == null) {
2306-
if (pos.getErrorIndex() < 0) {
2307-
pos.setErrorIndex(0);
2299+
if (context == null) {
2300+
if (pos.getErrorIndex() < 0) {
2301+
pos.setErrorIndex(0);
2302+
}
2303+
return null;
23082304
}
2309-
return null;
2310-
}
2311-
try {
23122305
TemporalAccessor resolved = context.toResolved(formatter.resolverStyle, formatter.resolverFields);
23132306
if (parseType == null) {
23142307
return resolved;
23152308
}
23162309
return resolved.query(parseType);
23172310
} catch (RuntimeException ex) {
2318-
pos.setErrorIndex(0);
2311+
if (pos.getErrorIndex() < 0) {
2312+
pos.setErrorIndex(0);
2313+
}
23192314
return null;
23202315
}
23212316
}
23222317
}
2323-
23242318
}

test/jdk/java/time/test/java/time/format/TestDateTimeParsing.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -62,15 +62,19 @@
6262
import static java.time.temporal.ChronoField.AMPM_OF_DAY;
6363
import static java.time.temporal.ChronoField.EPOCH_DAY;
6464
import static java.time.temporal.ChronoField.HOUR_OF_AMPM;
65+
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
6566
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
6667
import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
6768
import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
69+
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
6870
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
6971
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
7072
import static java.time.temporal.ChronoField.SECOND_OF_DAY;
7173
import static java.util.Locale.US;
7274
import static org.testng.Assert.assertEquals;
75+
import static org.testng.Assert.assertNull;
7376

77+
import java.text.ParsePosition;
7478
import java.time.DateTimeException;
7579
import java.time.Instant;
7680
import java.time.LocalDateTime;
@@ -80,15 +84,17 @@
8084
import java.time.format.DateTimeFormatter;
8185
import java.time.format.DateTimeFormatterBuilder;
8286
import java.time.format.DateTimeParseException;
87+
import java.time.format.SignStyle;
8388
import java.time.temporal.TemporalAccessor;
89+
import java.util.Locale;
8490

8591
import org.testng.annotations.DataProvider;
8692
import org.testng.annotations.Test;
8793

8894
/**
8995
* @test
9096
* @summary Test parsing of edge cases.
91-
* @bug 8223773 8272473
97+
* @bug 8223773 8272473 8319640
9298
*/
9399
public class TestDateTimeParsing {
94100

@@ -237,4 +243,30 @@ public void test_validateHourOfAmPm() {
237243
}
238244
}
239245
}
246+
247+
// Checks ::toFormat().parseObject(text, pos) do not throw DateTimeException
248+
@Test
249+
public void test_toFormat_2arg_null_return_on_DateTimeException() {
250+
var f = new DateTimeFormatterBuilder()
251+
.appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE)
252+
.optionalStart()
253+
.appendLiteral(':')
254+
.appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE)
255+
.optionalEnd()
256+
.optionalStart()
257+
.appendOffset("+HHmm", "Z")
258+
.optionalEnd()
259+
.toFormatter(Locale.ROOT)
260+
.toFormat();
261+
assertNull(f.parseObject("17-30", new ParsePosition(0)));
262+
}
263+
264+
// Checks ::toFormat().parseObject(text, pos) do not throw IOOBE
265+
@Test
266+
public void test_toFormat_2arg_null_return_on_IOOBE() {
267+
var date = "2023-11-13";
268+
assertNull(DateTimeFormatter.ISO_LOCAL_DATE
269+
.toFormat()
270+
.parseObject(date, new ParsePosition(date.length() + 1)));
271+
}
240272
}

0 commit comments

Comments
 (0)