001/* 002 * Copyright 2002-2016 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; 031 032/** 033 * {@link Parser} implementation for a JSR-310 {@link java.time.temporal.TemporalAccessor}, 034 * using a {@link java.time.format.DateTimeFormatter}) (the contextual one, if available). 035 * 036 * @author Juergen Hoeller 037 * @since 4.0 038 * @see DateTimeContextHolder#getFormatter 039 * @see java.time.LocalDate#parse(CharSequence, java.time.format.DateTimeFormatter) 040 * @see java.time.LocalTime#parse(CharSequence, java.time.format.DateTimeFormatter) 041 * @see java.time.LocalDateTime#parse(CharSequence, java.time.format.DateTimeFormatter) 042 * @see java.time.ZonedDateTime#parse(CharSequence, java.time.format.DateTimeFormatter) 043 * @see java.time.OffsetDateTime#parse(CharSequence, java.time.format.DateTimeFormatter) 044 * @see java.time.OffsetTime#parse(CharSequence, java.time.format.DateTimeFormatter) 045 */ 046public final class TemporalAccessorParser implements Parser<TemporalAccessor> { 047 048 private final Class<? extends TemporalAccessor> temporalAccessorType; 049 050 private final DateTimeFormatter formatter; 051 052 053 /** 054 * Create a new TemporalAccessorParser for the given TemporalAccessor type. 055 * @param temporalAccessorType the specific TemporalAccessor class 056 * (LocalDate, LocalTime, LocalDateTime, ZonedDateTime, OffsetDateTime, OffsetTime) 057 * @param formatter the base DateTimeFormatter instance 058 */ 059 public TemporalAccessorParser(Class<? extends TemporalAccessor> temporalAccessorType, DateTimeFormatter formatter) { 060 this.temporalAccessorType = temporalAccessorType; 061 this.formatter = formatter; 062 } 063 064 065 @Override 066 public TemporalAccessor parse(String text, Locale locale) throws ParseException { 067 DateTimeFormatter formatterToUse = DateTimeContextHolder.getFormatter(this.formatter, locale); 068 if (LocalDate.class == this.temporalAccessorType) { 069 return LocalDate.parse(text, formatterToUse); 070 } 071 else if (LocalTime.class == this.temporalAccessorType) { 072 return LocalTime.parse(text, formatterToUse); 073 } 074 else if (LocalDateTime.class == this.temporalAccessorType) { 075 return LocalDateTime.parse(text, formatterToUse); 076 } 077 else if (ZonedDateTime.class == this.temporalAccessorType) { 078 return ZonedDateTime.parse(text, formatterToUse); 079 } 080 else if (OffsetDateTime.class == this.temporalAccessorType) { 081 return OffsetDateTime.parse(text, formatterToUse); 082 } 083 else if (OffsetTime.class == this.temporalAccessorType) { 084 return OffsetTime.parse(text, formatterToUse); 085 } 086 else { 087 throw new IllegalStateException("Unsupported TemporalAccessor type: " + this.temporalAccessorType); 088 } 089 } 090 091}