View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
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  package org.supercsv.cellprocessor.joda;
17  
18  import java.util.Locale;
19  
20  import org.joda.time.DateTime;
21  import org.joda.time.format.DateTimeFormat;
22  import org.joda.time.format.DateTimeFormatter;
23  import org.joda.time.format.DateTimeFormatterBuilder;
24  import org.joda.time.format.ISODateTimeFormat;
25  import org.supercsv.cellprocessor.ift.CellProcessor;
26  
27  /**
28   * Converts a Joda DateTime to a String.
29   * <p>
30   * For constructors using DateTimeFormatter, refer to the following Joda
31   * classes:
32   * <ul>
33   * <li>{@link DateTimeFormat} - formats by pattern and style</li>
34   * <li>{@link ISODateTimeFormat} - ISO8601 formats</li>
35   * <li>{@link DateTimeFormatterBuilder} - complex formats created via method
36   * calls</li>
37   * </ul>
38   * <p>
39   * For constructors using date format Strings, refer to {@link DateTimeFormat}
40   * for example formats.
41   * 
42   * @since 2.3.0
43   * @author James Bassett
44   */
45  public class FmtDateTime extends AbstractJodaFormattingProcessor<DateTime> {
46  
47  	private static final Class<DateTime> JODA_CLASS = DateTime.class;
48  
49  	/**
50  	 * Constructs a new <tt>FmtDateTime</tt> processor, which formats a Joda
51  	 * DateTime as a String.
52  	 */
53  	public FmtDateTime() {
54  		super(JODA_CLASS);
55  	}
56  
57  	/**
58  	 * Constructs a new <tt>FmtDateTime</tt> processor, which formats a Joda
59  	 * DateTime as a String, then calls the next processor in the chain.
60  	 * 
61  	 * @param next
62  	 *            next processor in the chain
63  	 * @throws NullPointerException
64  	 *             if next is null
65  	 */
66  	public FmtDateTime(final CellProcessor next) {
67  		super(JODA_CLASS, next);
68  	}
69  
70  	/**
71  	 * Constructs a new <tt>FmtDateTime</tt> processor, which formats a Joda
72  	 * DateTime as a String using the supplied formatter.
73  	 * 
74  	 * @param formatter
75  	 *            the formatter to use
76  	 * @throws NullPointerException
77  	 *             if formatter is null
78  	 */
79  	public FmtDateTime(final DateTimeFormatter formatter) {
80  		super(JODA_CLASS, formatter);
81  	}
82  
83  	/**
84  	 * Constructs a new <tt>FmtDateTime</tt> processor, which formats a Joda
85  	 * DateTime as a String using the supplied formatter, then calls the next
86  	 * processor in the chain.
87  	 * 
88  	 * @param formatter
89  	 *            the formatter to use
90  	 * @param next
91  	 *            the next processor in the chain
92  	 * @throws NullPointerException
93  	 *             if formatter or next is null
94  	 */
95  	public FmtDateTime(final DateTimeFormatter formatter,
96  			final CellProcessor next) {
97  		super(JODA_CLASS, formatter, next);
98  	}
99  
100 	/**
101 	 * Constructs a new <tt>FmtDateTime</tt> processor, which formats a Joda
102 	 * DateTime as a String using the supplied pattern and the default locale.
103 	 * 
104 	 * @param pattern
105 	 *            the pattern to use
106 	 * @throws NullPointerException
107 	 *             if pattern is null
108 	 */
109 	public FmtDateTime(final String pattern) {
110 		super(JODA_CLASS, pattern);
111 	}
112 
113 	/**
114 	 * Constructs a new <tt>FmtDateTime</tt> processor, which formats a Joda
115 	 * DateTime as a String using the supplied pattern and the default locale,
116 	 * then calls the next processor in the chain.
117 	 * 
118 	 * @param pattern
119 	 *            the pattern to use
120 	 * @param next
121 	 *            the next processor in the chain
122 	 * @throws NullPointerException
123 	 *             if pattern or next is null
124 	 */
125 	public FmtDateTime(final String pattern, final CellProcessor next) {
126 		super(JODA_CLASS, pattern, next);
127 	}
128 
129 	/**
130 	 * Constructs a new <tt>FmtDateTime</tt> processor, which formats a Joda
131 	 * DateTime as a String using the supplied pattern and the locale.
132 	 * 
133 	 * @param pattern
134 	 *            the pattern to use
135 	 * @param locale
136 	 *            the locale to use (default used if <tt>null</tt>)
137 	 * @throws NullPointerException
138 	 *             if pattern is null
139 	 */
140 	public FmtDateTime(final String pattern, final Locale locale) {
141 		super(JODA_CLASS, pattern, locale);
142 	}
143 
144 	/**
145 	 * Constructs a new <tt>FmtDateTime</tt> processor, which formats a Joda
146 	 * DateTime as a String using the supplied pattern and the locale, then
147 	 * calls the next processor in the chain.
148 	 * 
149 	 * @param pattern
150 	 *            the pattern to use
151 	 * @param locale
152 	 *            the locale to use (default used if <tt>null</tt>)
153 	 * @param next
154 	 *            the next processor in the chain
155 	 * @throws NullPointerException
156 	 *             if pattern or next is null
157 	 */
158 	public FmtDateTime(final String pattern, final Locale locale,
159 			final CellProcessor next) {
160 		super(JODA_CLASS, pattern, locale, next);
161 	}
162 
163 	/**
164 	 * {@inheritDoc}
165 	 */
166 	@Override
167 	protected String format(final DateTime jodaType,
168 			final DateTimeFormatter formatter) {
169 		return jodaType.toString(formatter);
170 	}
171 
172 	/**
173 	 * {@inheritDoc}
174 	 */
175 	@Override
176 	protected String format(final DateTime jodaType, final String pattern,
177 			final Locale locale) {
178 		return jodaType.toString(pattern, locale);
179 	}
180 
181 	/**
182 	 * {@inheritDoc}
183 	 */
184 	@Override
185 	protected String format(final DateTime jodaType) {
186 		return jodaType.toString();
187 	}
188 
189 }