Java Code Examples for org.springframework.format.datetime.standard.TemporalAccessorParser

Following code examples demonstrate how to use org.springframework.format.datetime.standard.TemporalAccessorParserfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.format.datetime.standard.TemporalAccessorParserand various code implementation of this class.

	public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
		DateTimeFormatter formatter = configureFormatterFrom(annotation, fieldType);
		return new TemporalAccessorParser((Class<? extends TemporalAccessor>) fieldType, formatter);
	} 


                public T fromString(String string) {
                    try {
                        TemporalAccessorParser parser = new TemporalAccessorParser((Class<TemporalAccessor>) rawType, formatter);
                        return (T) parser.parse(string, Locale.US);
                    } catch (ParseException | IllegalStateException e) {
                        throw new BadRequestException(e);
                    }
                } 

	public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
		final TemporalAccessorParser parser = (TemporalAccessorParser) super.getParser(annotation, fieldType);

		Parser<TemporalAccessor> trimmingParser = new Parser<TemporalAccessor>() {
			@Override
			public TemporalAccessor parse(String text, Locale locale) throws ParseException {
				ZoneId userTimeZone = SecurityUtil.getCurrentUser().getTimeZone();
				TemporalAccessor x = parser.parse(text == null ? null : text.trim(), locale);

				if (x instanceof ZonedDateTime && userTimeZone != null) {
					x = ((ZonedDateTime) x).withZoneSameLocal(userTimeZone);
				}
				return x;
			}
		};

		return trimmingParser;
	} 

Advertisement
Javadoc
Parser implementation for a JSR-310 java.time.temporal.TemporalAccessor, using a java.time.format.Da

teTimeFormatter) (the contextual one, if available). @author Juergen Hoeller @since 4.0 @see DateTimeContextHolder#getFormatter @see java.time.LocalDate#parse(CharSequence, java.time.format.DateTimeFormatter) @see java.time.LocalTime#parse(CharSequence, java.time.format.DateTimeFormatter) @see java.time.LocalDateTime#parse(CharSequence, java.time.format.DateTimeFormatter) @see java.time.ZonedDateTime#parse(CharSequence, java.time.format.DateTimeFormatter) @see java.time.OffsetDateTime#parse(CharSequence, java.time.format.DateTimeFormatter) @see java.time.OffsetTime#parse(CharSequence, java.time.format.DateTimeFormatter)

Read More
Advertisement