1 /*
2  * Copyright 2002-2009 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.format.datetime.joda;
18 
19 import org.joda.time.Chronology;
20 import org.joda.time.DateTimeZone;
21 import org.joda.time.format.DateTimeFormatter;
22 
23 /**
24  * A context that holds user-specific Joda Time settings such as the user's Chronology (calendar system) and time zone.
25  * A <code>null</code> property value indicate the user has not specified a setting.
26  *
27  * @author Keith Donald
28  * @since 3.0
29  * @see JodaTimeContextHolder
30  */
31 public class JodaTimeContext {
32 
33 	private Chronology chronology;
34 
35 	private DateTimeZone timeZone;
36 
37 
38 	/**
39 	 * Set the user's chronology.
40 	 */
setChronology(Chronology chronology)41 	public void setChronology(Chronology chronology) {
42 		this.chronology = chronology;
43 	}
44 
45 	/**
46 	 * The user's chronology (calendar system).
47 	 * Null if not specified.
48 	 */
getChronology()49 	public Chronology getChronology() {
50 		return this.chronology;
51 	}
52 
53 	/**
54 	 * Set the user's timezone.
55 	 */
setTimeZone(DateTimeZone timeZone)56 	public void setTimeZone(DateTimeZone timeZone) {
57 		this.timeZone = timeZone;
58 	}
59 
60 	/**
61 	 * The user's timezone.
62 	 * Null if not specified.
63 	 */
getTimeZone()64 	public DateTimeZone getTimeZone() {
65 		return timeZone;
66 	}
67 
68 
69 	/**
70 	 * Gets the Formatter with the this context's settings applied to the base <code>formatter</code>.
71 	 * @param formatter the base formatter that establishes default formatting rules, generally context independent
72 	 * @return the context DateTimeFormatter
73 	 */
getFormatter(DateTimeFormatter formatter)74 	public DateTimeFormatter getFormatter(DateTimeFormatter formatter) {
75 		if (this.chronology != null) {
76 			formatter = formatter.withChronology(this.chronology);
77 		}
78 		if (this.timeZone != null) {
79 			formatter = formatter.withZone(this.timeZone);
80 		}
81 		return formatter;
82 	}
83 
84 }
85