Java Code Examples for java.time.format.DateTimeFormatterBuilder#toFormatter()
The following examples show how to use
java.time.format.DateTimeFormatterBuilder#toFormatter() .
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: TCKLocalizedFieldParser.java From j2objc with Apache License 2.0 | 6 votes |
@Test @UseDataProvider("provider_patternLocalDate") public void test_parse_textLocalDate(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) { ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertEquals("Incorrect ending parse position", ppos.getIndex(), expectedPos); assertEquals(parsed.isSupported(YEAR_OF_ERA), true); assertEquals(parsed.isSupported(WeekFields.of(locale).dayOfWeek()), true); assertEquals(parsed.isSupported(WeekFields.of(locale).weekOfMonth()) || parsed.isSupported(WeekFields.of(locale).weekOfYear()), true); // ensure combination resolves into a date LocalDate result = LocalDate.parse(text, dtf); assertEquals("LocalDate incorrect for " + pattern, result, expectedValue); } }
Example 2
Source File: TCKLocalizedFieldParser.java From hottub with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="LocalWeekMonthYearPatterns") public void test_parse_textLocalDate(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) { ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position"); assertEquals(parsed.isSupported(YEAR_OF_ERA), true); assertEquals(parsed.isSupported(WeekFields.of(locale).dayOfWeek()), true); assertEquals(parsed.isSupported(WeekFields.of(locale).weekOfMonth()) || parsed.isSupported(WeekFields.of(locale).weekOfYear()), true); // ensure combination resolves into a date LocalDate result = LocalDate.parse(text, dtf); assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern); } }
Example 3
Source File: TestReducedParser.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="ParseAdjacent") public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) { ParsePosition ppos = new ParsePosition(0); builder = new DateTimeFormatterBuilder(); setStrict(strict); builder.appendPattern(pattern); DateTimeFormatter dtf = builder.toFormatter(); TemporalAccessor parsed = dtf.parseUnresolved(input, ppos); assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf)); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position"); } else { assertEquals(ppos.getIndex(), parseLen, "parse position"); assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year)); assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month)); assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day)); } }
Example 4
Source File: TestReducedParser.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="ParseAdjacent") public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) { ParsePosition ppos = new ParsePosition(0); builder = new DateTimeFormatterBuilder(); setStrict(strict); builder.appendPattern(pattern); DateTimeFormatter dtf = builder.toFormatter(); TemporalAccessor parsed = dtf.parseUnresolved(input, ppos); assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf)); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position"); } else { assertEquals(ppos.getIndex(), parseLen, "parse position"); assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year)); assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month)); assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day)); } }
Example 5
Source File: TCKLocalizedFieldParser.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="LocalWeekMonthYearPatterns") public void test_parse_textLocalDate(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) { ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position"); assertEquals(parsed.isSupported(YEAR_OF_ERA), true); assertEquals(parsed.isSupported(WeekFields.of(locale).dayOfWeek()), true); assertEquals(parsed.isSupported(WeekFields.of(locale).weekOfMonth()) || parsed.isSupported(WeekFields.of(locale).weekOfYear()), true); // ensure combination resolves into a date LocalDate result = LocalDate.parse(text, dtf); assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern); } }
Example 6
Source File: JavaDateFormatter.java From crate with Apache License 2.0 | 6 votes |
@Override public DateFormatter parseDefaulting(Map<TemporalField, Long> fields) { final DateTimeFormatterBuilder parseDefaultingBuilder = new DateTimeFormatterBuilder().append(printer); fields.forEach(parseDefaultingBuilder::parseDefaulting); if (parsers.length == 1 && parsers[0].equals(printer)) { return new JavaDateFormatter(format, parseDefaultingBuilder.toFormatter(Locale.ROOT)); } else { final DateTimeFormatter[] parsersWithDefaulting = new DateTimeFormatter[parsers.length]; for (int i = 0; i < parsers.length; i++) { DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().append(parsers[i]); fields.forEach(builder::parseDefaulting); parsersWithDefaulting[i] = builder.toFormatter(Locale.ROOT); } return new JavaDateFormatter(format, parseDefaultingBuilder.toFormatter(Locale.ROOT), parsersWithDefaulting); } }
Example 7
Source File: TCKLocalizedFieldParser.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="LocalWeekBasedYearPatterns") public void test_parse_WeekBasedYear(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) { ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { WeekFields weekDef = WeekFields.of(locale); assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position"); assertEquals(parsed.isSupported(weekDef.dayOfWeek()), pattern.indexOf('e') >= 0); assertEquals(parsed.isSupported(weekDef.weekOfWeekBasedYear()), pattern.indexOf('w') >= 0); assertEquals(parsed.isSupported(weekDef.weekBasedYear()), pattern.indexOf('Y') >= 0); // ensure combination resolves into a date LocalDate result = LocalDate.parse(text, dtf); assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern + ", weekDef: " + weekDef); } }
Example 8
Source File: TCKLocalizedFieldParser.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="LocalWeekMonthYearPatterns") public void test_parse_textLocalDate(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) { ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position"); assertEquals(parsed.isSupported(YEAR_OF_ERA), true); assertEquals(parsed.isSupported(WeekFields.of(locale).dayOfWeek()), true); assertEquals(parsed.isSupported(WeekFields.of(locale).weekOfMonth()) || parsed.isSupported(WeekFields.of(locale).weekOfYear()), true); // ensure combination resolves into a date LocalDate result = LocalDate.parse(text, dtf); assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern); } }
Example 9
Source File: TestReducedParser.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="ParseAdjacent") public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) { ParsePosition ppos = new ParsePosition(0); builder = new DateTimeFormatterBuilder(); setStrict(strict); builder.appendPattern(pattern); DateTimeFormatter dtf = builder.toFormatter(); TemporalAccessor parsed = dtf.parseUnresolved(input, ppos); assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf)); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position"); } else { assertEquals(ppos.getIndex(), parseLen, "parse position"); assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year)); assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month)); assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day)); } }
Example 10
Source File: TCKLocalizedFieldParser.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="LocalWeekBasedYearPatterns") public void test_parse_WeekBasedYear(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) { ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { WeekFields weekDef = WeekFields.of(locale); assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position"); assertEquals(parsed.isSupported(weekDef.dayOfWeek()), pattern.indexOf('e') >= 0); assertEquals(parsed.isSupported(weekDef.weekOfWeekBasedYear()), pattern.indexOf('w') >= 0); assertEquals(parsed.isSupported(weekDef.weekBasedYear()), pattern.indexOf('Y') >= 0); // ensure combination resolves into a date LocalDate result = LocalDate.parse(text, dtf); assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern + ", weekDef: " + weekDef); } }
Example 11
Source File: TestReducedParser.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="ParseAdjacent") public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) { ParsePosition ppos = new ParsePosition(0); builder = new DateTimeFormatterBuilder(); setStrict(strict); builder.appendPattern(pattern); DateTimeFormatter dtf = builder.toFormatter(); TemporalAccessor parsed = dtf.parseUnresolved(input, ppos); assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf)); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position"); } else { assertEquals(ppos.getIndex(), parseLen, "parse position"); assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year)); assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month)); assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day)); } }
Example 12
Source File: TCKLocalizedFieldParser.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="FieldPatterns") public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) { WeekFields weekDef = WeekFields.of(locale); TemporalField field = null; switch(pattern.charAt(0)) { case 'e' : field = weekDef.dayOfWeek(); break; case 'w': field = weekDef.weekOfWeekBasedYear(); break; case 'W': field = weekDef.weekOfMonth(); break; case 'Y': field = weekDef.weekBasedYear(); break; default: throw new IllegalStateException("bad format letter from pattern"); } ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position"); long value = parsed.getLong(field); assertEquals(value, expectedValue, "Value incorrect for " + field); } }
Example 13
Source File: TCKLocalizedFieldParser.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="FieldPatterns") public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) { WeekFields weekDef = WeekFields.of(locale); TemporalField field = null; switch(pattern.charAt(0)) { case 'c' : case 'e' : field = weekDef.dayOfWeek(); break; case 'w': field = weekDef.weekOfWeekBasedYear(); break; case 'W': field = weekDef.weekOfMonth(); break; case 'Y': field = weekDef.weekBasedYear(); break; default: throw new IllegalStateException("bad format letter from pattern"); } ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position"); long value = parsed.getLong(field); assertEquals(value, expectedValue, "Value incorrect for " + field); } }
Example 14
Source File: TestReducedPrinter.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="PrintAdjacent") public void test_printAdjacent(String pattern, String text, int year, int month, int day) { builder = new DateTimeFormatterBuilder(); builder.appendPattern(pattern); DateTimeFormatter dtf = builder.toFormatter(); LocalDate ld = LocalDate.of(year, month, day); String actual = dtf.format(ld); assertEquals(actual, text, "formatter output: " + dtf); }
Example 15
Source File: DateTimeFormater.java From JavaSE8-Features with GNU General Public License v3.0 | 5 votes |
public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); DateTimeFormatter df = DateTimeFormatter.ISO_DATE; System.out.println(df.format(currentDate)); LocalTime currentTime = LocalTime.now(); DateTimeFormatter dt = DateTimeFormatter.ISO_TIME; System.out.println(dt.format(currentTime)); LocalDateTime currentDT = LocalDateTime.now(); DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME; System.out.println(dtf.format(currentDT)); DateTimeFormatter f_long = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG); System.out.println(f_long.format(currentDT)); DateTimeFormatter f_short = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); System.out.println(f_short.format(currentDT)); String fr_short = f_short.withLocale(Locale.FRENCH).format(currentDT); String fr_long = f_long.withLocale(Locale.FRENCH).format(currentDT); System.out.println(fr_short); System.out.println(fr_long); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder() .appendValue(ChronoField.DAY_OF_YEAR) .appendLiteral("||") .appendValue(ChronoField.DAY_OF_MONTH) .appendLiteral("||") .appendValue(ChronoField.YEAR); DateTimeFormatter f = b.toFormatter(); System.out.println(f.format(currentDT)); }
Example 16
Source File: TestReducedPrinter.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="PrintAdjacent") public void test_printAdjacent(String pattern, String text, int year, int month, int day) { builder = new DateTimeFormatterBuilder(); builder.appendPattern(pattern); DateTimeFormatter dtf = builder.toFormatter(); LocalDate ld = LocalDate.of(year, month, day); String actual = dtf.format(ld); assertEquals(actual, text, "formatter output: " + dtf); }
Example 17
Source File: TestReducedPrinter.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="PrintAdjacent") public void test_printAdjacent(String pattern, String text, int year, int month, int day) { builder = new DateTimeFormatterBuilder(); builder.appendPattern(pattern); DateTimeFormatter dtf = builder.toFormatter(); LocalDate ld = LocalDate.of(year, month, day); String actual = dtf.format(ld); assertEquals(actual, text, "formatter output: " + dtf); }
Example 18
Source File: TCKLocalizedFieldParser.java From hottub with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="FieldPatterns") public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) { WeekFields weekDef = WeekFields.of(locale); TemporalField field = null; switch(pattern.charAt(0)) { case 'e' : field = weekDef.dayOfWeek(); break; case 'w': field = weekDef.weekOfWeekBasedYear(); break; case 'W': field = weekDef.weekOfMonth(); break; case 'Y': field = weekDef.weekBasedYear(); break; default: throw new IllegalStateException("bad format letter from pattern"); } ParsePosition ppos = new ParsePosition(pos); DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern); DateTimeFormatter dtf = b.toFormatter(locale); TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position"); long value = parsed.getLong(field); assertEquals(value, expectedValue, "Value incorrect for " + field); } }
Example 19
Source File: TestReducedPrinter.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="PrintAdjacent") public void test_printAdjacent(String pattern, String text, int year, int month, int day) { builder = new DateTimeFormatterBuilder(); builder.appendPattern(pattern); DateTimeFormatter dtf = builder.toFormatter(); LocalDate ld = LocalDate.of(year, month, day); String actual = dtf.format(ld); assertEquals(actual, text, "formatter output: " + dtf); }
Example 20
Source File: TestReducedPrinter.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="PrintAdjacent") public void test_printAdjacent(String pattern, String text, int year, int month, int day) { builder = new DateTimeFormatterBuilder(); builder.appendPattern(pattern); DateTimeFormatter dtf = builder.toFormatter(); LocalDate ld = LocalDate.of(year, month, day); String actual = dtf.format(ld); assertEquals(actual, text, "formatter output: " + dtf); }