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.ZoneId; 020import java.time.chrono.Chronology; 021import java.time.format.DateTimeFormatter; 022import java.util.TimeZone; 023 024import org.springframework.context.i18n.LocaleContext; 025import org.springframework.context.i18n.LocaleContextHolder; 026import org.springframework.context.i18n.TimeZoneAwareLocaleContext; 027import org.springframework.lang.UsesJava8; 028 029/** 030 * A context that holds user-specific <code>java.time</code> (JSR-310) settings 031 * such as the user's Chronology (calendar system) and time zone. 032 * A {@code null} property value indicate the user has not specified a setting. 033 * 034 * @author Juergen Hoeller 035 * @since 4.0 036 * @see DateTimeContextHolder 037 */ 038@UsesJava8 039public class DateTimeContext { 040 041 private Chronology chronology; 042 043 private ZoneId timeZone; 044 045 046 /** 047 * Set the user's chronology (calendar system). 048 */ 049 public void setChronology(Chronology chronology) { 050 this.chronology = chronology; 051 } 052 053 /** 054 * Return the user's chronology (calendar system), if any. 055 */ 056 public Chronology getChronology() { 057 return this.chronology; 058 } 059 060 /** 061 * Set the user's time zone. 062 * <p>Alternatively, set a {@link TimeZoneAwareLocaleContext} on 063 * {@link LocaleContextHolder}. This context class will fall back to 064 * checking the locale context if no setting has been provided here. 065 * @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone() 066 * @see org.springframework.context.i18n.LocaleContextHolder#setLocaleContext 067 */ 068 public void setTimeZone(ZoneId timeZone) { 069 this.timeZone = timeZone; 070 } 071 072 /** 073 * Return the user's time zone, if any. 074 */ 075 public ZoneId getTimeZone() { 076 return this.timeZone; 077 } 078 079 080 /** 081 * Get the DateTimeFormatter with the this context's settings 082 * applied to the base {@code formatter}. 083 * @param formatter the base formatter that establishes default 084 * formatting rules, generally context-independent 085 * @return the contextual DateTimeFormatter 086 */ 087 public DateTimeFormatter getFormatter(DateTimeFormatter formatter) { 088 if (this.chronology != null) { 089 formatter = formatter.withChronology(this.chronology); 090 } 091 if (this.timeZone != null) { 092 formatter = formatter.withZone(this.timeZone); 093 } 094 else { 095 LocaleContext localeContext = LocaleContextHolder.getLocaleContext(); 096 if (localeContext instanceof TimeZoneAwareLocaleContext) { 097 TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone(); 098 if (timeZone != null) { 099 formatter = formatter.withZone(timeZone.toZoneId()); 100 } 101 } 102 } 103 return formatter; 104 } 105 106}