Java Code Examples for java.time.format.DateTimeFormatter#withResolverStyle()

The following examples show how to use java.time.format.DateTimeFormatter#withResolverStyle() . 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: TCKResolverStyle.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 2
Source File: TCKResolverStyle.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 3
Source File: TestNonIsoFormatter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="lenient_eraYear")
    public void test_lenientEraYear(Chronology chrono, String lenient, String strict) {
        String mdStr = "-01-01";
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
            .appendPattern("GGGG y-M-d")
            .toFormatter(Locale.ROOT)
            .withChronology(chrono);
        DateTimeFormatter dtfLenient = dtf.withResolverStyle(ResolverStyle.LENIENT);
        assertEquals(LocalDate.parse(lenient+mdStr, dtfLenient), LocalDate.parse(strict+mdStr, dtf));
}
 
Example 4
Source File: TCKResolverStyle.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 5
Source File: TCKResolverStyle.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 6
Source File: TCKResolverStyle.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 7
Source File: TCKResolverStyle.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("data_resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 8
Source File: TCKResolverStyle.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 9
Source File: TCKResolverStyle.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 10
Source File: ComAdminImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public DateTimeFormatter getDateTimeFormatterWithSeconds() {
	String dateFormatPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.MEDIUM, IsoChronology.INSTANCE, getLocale());
	dateFormatPattern = dateFormatPattern.replaceFirst("y+", "yyyy").replaceFirst(", ", " ");
	DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormatPattern, getLocale());
	dateTimeFormatter.withZone(TimeZone.getTimeZone(getAdminTimezone()).toZoneId());
	dateTimeFormatter.withResolverStyle(ResolverStyle.STRICT);
	return dateTimeFormatter;
}
 
Example 11
Source File: TestNonIsoFormatter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="lenient_eraYear")
    public void test_lenientEraYear(Chronology chrono, String lenient, String strict) {
        String mdStr = "-01-01";
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
            .appendPattern("GGGG y-M-d")
            .toFormatter(Locale.ROOT)
            .withChronology(chrono);
        DateTimeFormatter dtfLenient = dtf.withResolverStyle(ResolverStyle.LENIENT);
        assertEquals(LocalDate.parse(lenient+mdStr, dtfLenient), LocalDate.parse(strict+mdStr, dtf));
}
 
Example 12
Source File: TCKResolverStyle.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 13
Source File: TCKResolverStyle.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 14
Source File: TestNonIsoFormatter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="lenient_eraYear")
    public void test_lenientEraYear(Chronology chrono, String lenient, String strict) {
        String mdStr = "-01-01";
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
            .appendPattern("GGGG y-M-d")
            .toFormatter(Locale.ROOT)
            .withChronology(chrono);
        DateTimeFormatter dtfLenient = dtf.withResolverStyle(ResolverStyle.LENIENT);
        assertEquals(LocalDate.parse(lenient+mdStr, dtfLenient), LocalDate.parse(strict+mdStr, dtf));
}
 
Example 15
Source File: TCKResolverStyle.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 16
Source File: TestNonIsoFormatter.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="lenient_eraYear")
    public void test_lenientEraYear(Chronology chrono, String lenient, String strict) {
        String mdStr = "-01-01";
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
            .appendPattern("GGGG y-M-d")
            .toFormatter(Locale.ROOT)
            .withChronology(chrono);
        DateTimeFormatter dtfLenient = dtf.withResolverStyle(ResolverStyle.LENIENT);
        assertEquals(LocalDate.parse(lenient+mdStr, dtfLenient), LocalDate.parse(strict+mdStr, dtf));
}
 
Example 17
Source File: TCKResolverStyle.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendValue(ChronoField.YEAR_OF_ERA);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.MONTH_OF_YEAR);
    builder.appendLiteral("/");
    builder.appendValue(ChronoField.DAY_OF_MONTH);

    Map<Long, String> eraMap = new HashMap<Long, String>();
    eraMap.put(1L, "CE");
    eraMap.put(0L, "BCE");
    DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();

    DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
    formatter = formatter.withResolverStyle(style);
    if (expectedEx == null) {
        TemporalAccessor accessor = formatter.parse(str);
        assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
        assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
        assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
    } else {
        try {
            formatter.parse(str);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 18
Source File: ComAdminImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public DateTimeFormatter getDateFormatter() {
	String dateFormatPattern = ((SimpleDateFormat) SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT, getLocale())).toPattern().replaceFirst("y+", "yyyy");
	DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(dateFormatPattern, getLocale());
	dateFormatter.withZone(TimeZone.getTimeZone(getAdminTimezone()).toZoneId());
	dateFormatter.withResolverStyle(ResolverStyle.STRICT);
	return dateFormatter;
}
 
Example 19
Source File: ComAdminImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public DateTimeFormatter getDateTimeFormatter() {
	String dateTimeFormatPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, IsoChronology.INSTANCE, getLocale());
	dateTimeFormatPattern = dateTimeFormatPattern.replaceFirst("y+", "yyyy").replaceFirst(", ", " ");
	DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeFormatPattern, getLocale());
	dateTimeFormatter.withZone(TimeZone.getTimeZone(getAdminTimezone()).toZoneId());
	dateTimeFormatter.withResolverStyle(ResolverStyle.STRICT);
	return dateTimeFormatter;
}
 
Example 20
Source File: JSR310DateTimeDeserializerBase.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
        BeanProperty property) throws JsonMappingException
{
    JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
    JSR310DateTimeDeserializerBase<?> deser = this;
    if (format != null) {
        // 17-Aug-2019, tatu: For 2.10 let's start considering leniency/strictness too
        if (format.hasLenient()) {
            Boolean leniency = format.getLenient();
            if (leniency != null) {
                deser = deser.withLeniency(leniency);
            }
        }
        if (format.hasPattern()) {
            final String pattern = format.getPattern();
            final Locale locale = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
            DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
            if (acceptCaseInsensitiveValues(ctxt, format)) {
                builder.parseCaseInsensitive();
            }
            builder.appendPattern(pattern);
            DateTimeFormatter df;
            if (locale == null) {
                df = builder.toFormatter();
            } else {
                df = builder.toFormatter(locale);
            }

            // [#148]: allow strict parsing
            if (!deser.isLenient()) {
                df = df.withResolverStyle(ResolverStyle.STRICT);
            }

            // [#69]: For instant serializers/deserializers we need to configure the formatter with
            //a time zone picked up from JsonFormat annotation, otherwise serialization might not work
            if (format.hasTimeZone()) {
                df = df.withZone(format.getTimeZone().toZoneId());
            }
            deser = deser.withDateFormat(df);
        }
        // [#58]: For LocalDate deserializers we need to configure the formatter with
        //a shape picked up from JsonFormat annotation, to decide if the value is EpochSeconds
        JsonFormat.Shape shape = format.getShape();
        if (shape != null && shape != _shape) {
            deser = deser.withShape(shape);
        }
        // any use for TimeZone?
    }
    return deser;
}