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.time.format.DateTimeFormatter;
020import java.time.temporal.TemporalAccessor;
021import java.util.Locale;
022
023import org.springframework.format.Printer;
024import org.springframework.lang.UsesJava8;
025
026/**
027 * {@link Printer} implementation for a JSR-310 {@link java.time.temporal.TemporalAccessor},
028 * using a {@link java.time.format.DateTimeFormatter}) (the contextual one, if available).
029 *
030 * @author Juergen Hoeller
031 * @since 4.0
032 * @see DateTimeContextHolder#getFormatter
033 * @see java.time.format.DateTimeFormatter#format(java.time.temporal.TemporalAccessor)
034 */
035@UsesJava8
036public final class TemporalAccessorPrinter implements Printer<TemporalAccessor> {
037
038        private final DateTimeFormatter formatter;
039
040
041        /**
042         * Create a new TemporalAccessorPrinter.
043         * @param formatter the base DateTimeFormatter instance
044         */
045        public TemporalAccessorPrinter(DateTimeFormatter formatter) {
046                this.formatter = formatter;
047        }
048
049
050        @Override
051        public String print(TemporalAccessor partial, Locale locale) {
052                return DateTimeContextHolder.getFormatter(this.formatter, locale).format(partial);
053        }
054
055}