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