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