Java Code Examples for java.time.format.DateTimeFormatter#parseBest()
The following examples show how to use
java.time.format.DateTimeFormatter#parseBest() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TCKDateTimeFormatter.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void test_parseBest_firstOption() throws Exception { DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[XXX]"); TemporalAccessor result = test.parseBest("2011-06-30 12:30+03:00", ZonedDateTime::from, LocalDateTime::from); LocalDateTime ldt = LocalDateTime.of(2011, 6, 30, 12, 30); assertEquals(result, ZonedDateTime.of(ldt, ZoneOffset.ofHours(3))); }
Example 2
Source File: TCKDateTimeFormatter.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@Test public void test_parseBest_firstOption() throws Exception { DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[XXX]"); TemporalAccessor result = test.parseBest("2011-06-30 12:30+03:00", ZonedDateTime::from, LocalDateTime::from); LocalDateTime ldt = LocalDateTime.of(2011, 6, 30, 12, 30); assertEquals(result, ZonedDateTime.of(ldt, ZoneOffset.ofHours(3))); }
Example 3
Source File: TCKDateTimeFormatter.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
@Test(expectedExceptions=DateTimeParseException.class) public void test_parseBest_String_parseError() throws Exception { DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[XXX]"); try { test.parseBest("2011-06-XX", ZonedDateTime::from, LocalDateTime::from); } catch (DateTimeParseException ex) { assertEquals(ex.getMessage().contains("could not be parsed"), true); assertEquals(ex.getMessage().contains("XX"), true); assertEquals(ex.getParsedString(), "2011-06-XX"); assertEquals(ex.getErrorIndex(), 8); throw ex; } }
Example 4
Source File: TCKDateTimeFormatter.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
@Test(expectedExceptions=DateTimeParseException.class) public void test_parseBest_String_parseIncomplete() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); try { test.parseBest("ONE30SomethingElse", ZonedDateTime::from, LocalDate::from); } catch (DateTimeParseException ex) { assertEquals(ex.getMessage().contains("could not be parsed"), true); assertEquals(ex.getMessage().contains("ONE30SomethingElse"), true); assertEquals(ex.getParsedString(), "ONE30SomethingElse"); assertEquals(ex.getErrorIndex(), 5); throw ex; } }
Example 5
Source File: TCKDateTimeFormatter.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@Test(expectedExceptions=DateTimeParseException.class) public void test_parseBest_String_parseError() throws Exception { DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[XXX]"); try { test.parseBest("2011-06-XX", ZonedDateTime::from, LocalDateTime::from); } catch (DateTimeParseException ex) { assertEquals(ex.getMessage().contains("could not be parsed"), true); assertEquals(ex.getMessage().contains("XX"), true); assertEquals(ex.getParsedString(), "2011-06-XX"); assertEquals(ex.getErrorIndex(), 8); throw ex; } }
Example 6
Source File: TCKDateTimeFormatter.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
@Test public void test_parseBest_firstOption() throws Exception { DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[XXX]"); TemporalAccessor result = test.parseBest("2011-06-30 12:30+03:00", ZonedDateTime::from, LocalDateTime::from); LocalDateTime ldt = LocalDateTime.of(2011, 6, 30, 12, 30); assertEquals(result, ZonedDateTime.of(ldt, ZoneOffset.ofHours(3))); }
Example 7
Source File: TCKDateTimeFormatter.java From j2objc with Apache License 2.0 | 5 votes |
@Test(expected=DateTimeParseException.class) public void test_parseBest_String_parseErrorLongText() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); try { test.parseBest("ONEXXX67890123456789012345678901234567890123456789012345678901234567890123456789", ZonedDateTime::from, LocalDate::from); } catch (DateTimeParseException ex) { assertEquals(ex.getMessage().contains("could not be parsed"), true); assertEquals(ex.getMessage().contains("ONEXXX6789012345678901234567890123456789012345678901234567890123..."), true); assertEquals(ex.getParsedString(), "ONEXXX67890123456789012345678901234567890123456789012345678901234567890123456789"); assertEquals(ex.getErrorIndex(), 3); throw ex; } }
Example 8
Source File: TCKDateTimeFormatter.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test public void test_parseBest_firstOption() throws Exception { DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[XXX]"); TemporalAccessor result = test.parseBest("2011-06-30 12:30+03:00", ZonedDateTime::from, LocalDateTime::from); LocalDateTime ldt = LocalDateTime.of(2011, 6, 30, 12, 30); assertEquals(result, ZonedDateTime.of(ldt, ZoneOffset.ofHours(3))); }
Example 9
Source File: TCKDateTimeFormatter.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
@Test(expectedExceptions=DateTimeParseException.class) public void test_parseBest_String_parseIncomplete() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); try { test.parseBest("ONE30SomethingElse", ZonedDateTime::from, LocalDate::from); } catch (DateTimeParseException ex) { assertEquals(ex.getMessage().contains("could not be parsed"), true); assertEquals(ex.getMessage().contains("ONE30SomethingElse"), true); assertEquals(ex.getParsedString(), "ONE30SomethingElse"); assertEquals(ex.getErrorIndex(), 5); throw ex; } }
Example 10
Source File: FlexibleDateTimeParser.java From emissary with Apache License 2.0 | 5 votes |
/** * Attempts to parse a string date * * @param dateString the string to parse * @param formats the date/time formats to use * @return the parsed immutable and thread-safe zoned-date, or null if it failed to parse */ public static ZonedDateTime parse(final String dateString, final List<DateTimeFormatter> formats) { String cleanedDateString = cleanDateString(dateString); if (StringUtils.isBlank(cleanedDateString) || CollectionUtils.isEmpty(formats)) { return null; } for (DateTimeFormatter formatter : formats) { try { // try for a zoned date (has timezone), local date time (no time zone), or just a local date (no time) TemporalAccessor accessor = formatter.parseBest(cleanedDateString, ZonedDateTime::from, LocalDateTime::from, LocalDate::from); if (accessor instanceof ZonedDateTime) { return (ZonedDateTime) accessor; // return the date time w/ timezone } else if (accessor instanceof LocalDateTime) { return ((LocalDateTime) accessor).atZone(timezone); // set the timezone } else if (accessor instanceof LocalDate) { return ((LocalDate) accessor).atStartOfDay(timezone); // add zeroed out time } } catch (NullPointerException | IllegalArgumentException | DateTimeParseException e) { // Ignore b/c failures are expected -> set to trace otherwise will be noisy logger.trace("Error parsing date {} with format {}", dateString, formatter == null ? null : formatter.toString()); } } return null; }
Example 11
Source File: TCKDateTimeFormatter.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@Test public void test_parseBest_firstOption() throws Exception { DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[XXX]"); TemporalAccessor result = test.parseBest("2011-06-30 12:30+03:00", ZonedDateTime::from, LocalDateTime::from); LocalDateTime ldt = LocalDateTime.of(2011, 6, 30, 12, 30); assertEquals(result, ZonedDateTime.of(ldt, ZoneOffset.ofHours(3))); }
Example 12
Source File: TCKDateTimeFormatter.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@Test(expectedExceptions=DateTimeParseException.class) public void test_parseBest_String_parseErrorLongText() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); try { test.parseBest("ONEXXX67890123456789012345678901234567890123456789012345678901234567890123456789", ZonedDateTime::from, LocalDate::from); } catch (DateTimeParseException ex) { assertEquals(ex.getMessage().contains("could not be parsed"), true); assertEquals(ex.getMessage().contains("ONEXXX6789012345678901234567890123456789012345678901234567890123..."), true); assertEquals(ex.getParsedString(), "ONEXXX67890123456789012345678901234567890123456789012345678901234567890123456789"); assertEquals(ex.getErrorIndex(), 3); throw ex; } }
Example 13
Source File: TCKDateTimeFormatter.java From hottub with GNU General Public License v2.0 | 5 votes |
@Test public void test_parseBest_firstOption() throws Exception { DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[XXX]"); TemporalAccessor result = test.parseBest("2011-06-30 12:30+03:00", ZonedDateTime::from, LocalDateTime::from); LocalDateTime ldt = LocalDateTime.of(2011, 6, 30, 12, 30); assertEquals(result, ZonedDateTime.of(ldt, ZoneOffset.ofHours(3))); }
Example 14
Source File: DateProcessor.java From sawmill with Apache License 2.0 | 5 votes |
private ZonedDateTime getZonedDateTime(String value, DateTimeFormatter formatter) { TemporalAccessor temporal = formatter.parseBest(value, ZonedDateTime::from, LocalDateTime::from); if (temporal instanceof LocalDateTime) { if (timeZone == null) { return ZonedDateTime.of((LocalDateTime) temporal, ZoneOffset.UTC); } else { return ZonedDateTime.of((LocalDateTime) temporal, timeZone); } } else { return ZonedDateTime.from(temporal); } }
Example 15
Source File: TCKDateTimeFormatter.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
@Test public void test_parseBest_secondOption() throws Exception { DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd[ HH:mm[XXX]]"); TemporalAccessor result = test.parseBest("2011-06-30", ZonedDateTime::from, LocalDate::from); assertEquals(result, LocalDate.of(2011, 6, 30)); }
Example 16
Source File: TCKDateTimeFormatter.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
@Test(expectedExceptions=IllegalArgumentException.class) public void test_parseBest_String_oneRule() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); test.parseBest("30", LocalDate::from); }
Example 17
Source File: TCKDateTimeFormatter.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@Test(expectedExceptions=NullPointerException.class) public void test_parseBest_String_nullText() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); test.parseBest((String) null, ZonedDateTime::from, LocalDate::from); }
Example 18
Source File: TCKDateTimeFormatter.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@Test(expectedExceptions=IllegalArgumentException.class) public void test_parseBest_String_zeroRules() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); test.parseBest("30", new TemporalQuery<?>[0]); }
Example 19
Source File: TCKDateTimeFormatter.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
@Test(expectedExceptions=NullPointerException.class) public void test_parseBest_String_nullText() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); test.parseBest((String) null, ZonedDateTime::from, LocalDate::from); }
Example 20
Source File: TCKDateTimeFormatter.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
@Test public void test_parseBest_secondOption() throws Exception { DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd[ HH:mm[XXX]]"); TemporalAccessor result = test.parseBest("2011-06-30", ZonedDateTime::from, LocalDate::from); assertEquals(result, LocalDate.of(2011, 6, 30)); }