Skip to content

Commit

Permalink
Adjusted the parseDate method to catch DateTimeException instead of s…
Browse files Browse the repository at this point in the history
…eparately catching DateTimeParseException and DateTimeException. Now, the method correctly handles all cases of date parsing and conversion issues, including those related to missing time-zone information, by returning null as specified in the documentation.
  • Loading branch information
arturobernalg committed Feb 23, 2024
1 parent 8a7f707 commit f2cc6e2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@

package org.apache.hc.client5.http.utils;

import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
Expand Down Expand Up @@ -165,7 +165,7 @@ public static Instant parseDate(final String dateValue, final DateTimeFormatter.
for (final DateTimeFormatter dateFormatter : dateFormatters) {
try {
return Instant.from(dateFormatter.parse(v));
} catch (final DateTimeParseException ignore) {
} catch (final DateTimeException ignore) {
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void testDateParseMessage() throws Exception {
@Test
public void testMalformedDate() {
Assertions.assertNull(DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", new DateTimeFormatter[] {}));
Assertions.assertNull(DateUtils.parseDate("Thu Feb 22 17:20:18 2024", new DateTimeFormatter[] {}));
}

@Test
Expand Down

0 comments on commit f2cc6e2

Please sign in to comment.