001/* 002 * Copyright 2002-2013 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; 028 029/** 030 * A context that holds user-specific Joda-Time settings such as the user's 031 * Chronology (calendar system) and time zone. 032 * 033 * <p>A {@code null} property value indicate the user has not specified a setting. 034 * 035 * @author Keith Donald 036 * @since 3.0 037 * @see JodaTimeContextHolder 038 */ 039public class JodaTimeContext { 040 041 private Chronology chronology; 042 043 private DateTimeZone 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(DateTimeZone timeZone) { 069 this.timeZone = timeZone; 070 } 071 072 /** 073 * Return the user's time zone, if any. 074 */ 075 public DateTimeZone 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(DateTimeZone.forTimeZone(timeZone)); 100 } 101 } 102 } 103 return formatter; 104 } 105 106}