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