001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.format.datetime.standard;
018
019import java.text.ParseException;
020import java.time.LocalDate;
021import java.time.LocalDateTime;
022import java.time.LocalTime;
023import java.time.OffsetDateTime;
024import java.time.OffsetTime;
025import java.time.ZonedDateTime;
026import java.time.format.DateTimeFormatter;
027import java.time.temporal.TemporalAccessor;
028import java.util.Locale;
029
030import org.springframework.format.Parser;
031import org.springframework.lang.UsesJava8;
032
033/**
034 * {@link Parser} implementation for a JSR-310 {@link java.time.temporal.TemporalAccessor},
035 * using a {@link java.time.format.DateTimeFormatter}) (the contextual one, if available).
036 *
037 * @author Juergen Hoeller
038 * @since 4.0
039 * @see DateTimeContextHolder#getFormatter
040 * @see java.time.LocalDate#parse(CharSequence, java.time.format.DateTimeFormatter)
041 * @see java.time.LocalTime#parse(CharSequence, java.time.format.DateTimeFormatter)
042 * @see java.time.LocalDateTime#parse(CharSequence, java.time.format.DateTimeFormatter)
043 * @see java.time.ZonedDateTime#parse(CharSequence, java.time.format.DateTimeFormatter)
044 * @see java.time.OffsetDateTime#parse(CharSequence, java.time.format.DateTimeFormatter)
045 * @see java.time.OffsetTime#parse(CharSequence, java.time.format.DateTimeFormatter)
046 */
047@UsesJava8
048public final class TemporalAccessorParser implements Parser<TemporalAccessor> {
049
050        private final Class<? extends TemporalAccessor> temporalAccessorType;
051
052        private final DateTimeFormatter formatter;
053
054
055        /**
056         * Create a new TemporalAccessorParser for the given TemporalAccessor type.
057         * @param temporalAccessorType the specific TemporalAccessor class
058         * (LocalDate, LocalTime, LocalDateTime, ZonedDateTime, OffsetDateTime, OffsetTime)
059         * @param formatter the base DateTimeFormatter instance
060         */
061        public TemporalAccessorParser(Class<? extends TemporalAccessor> temporalAccessorType, DateTimeFormatter formatter) {
062                this.temporalAccessorType = temporalAccessorType;
063                this.formatter = formatter;
064        }
065
066
067        @Override
068        public TemporalAccessor parse(String text, Locale locale) throws ParseException {
069                DateTimeFormatter formatterToUse = DateTimeContextHolder.getFormatter(this.formatter, locale);
070                if (LocalDate.class == this.temporalAccessorType) {
071                        return LocalDate.parse(text, formatterToUse);
072                }
073                else if (LocalTime.class == this.temporalAccessorType) {
074                        return LocalTime.parse(text, formatterToUse);
075                }
076                else if (LocalDateTime.class == this.temporalAccessorType) {
077                        return LocalDateTime.parse(text, formatterToUse);
078                }
079                else if (ZonedDateTime.class == this.temporalAccessorType) {
080                        return ZonedDateTime.parse(text, formatterToUse);
081                }
082                else if (OffsetDateTime.class == this.temporalAccessorType) {
083                        return OffsetDateTime.parse(text, formatterToUse);
084                }
085                else if (OffsetTime.class == this.temporalAccessorType) {
086                        return OffsetTime.parse(text, formatterToUse);
087                }
088                else {
089                        throw new IllegalStateException("Unsupported TemporalAccessor type: " + this.temporalAccessorType);
090                }
091        }
092
093}