1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang.time;
  55. import java.util.Date;
  56. import java.util.Locale;
  57. import java.util.TimeZone;
  58. /**
  59. * <p>Date and time formatting utilites and constants.</p>
  60. *
  61. * <p>Formatting is performed using the
  62. * {@link org.apache.commons.lang.time.FastDateFormat} class.</p>
  63. *
  64. * @author Apache Ant - DateUtils
  65. * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
  66. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  67. * @author Stephen Colebourne
  68. * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
  69. * @since 2.0
  70. * @version $Id: DateFormatUtils.java,v 1.7 2003/08/18 02:22:25 bayard Exp $
  71. */
  72. public class DateFormatUtils {
  73. /**
  74. * ISO8601 formatter for date-time witout time zone.
  75. * The format used is <tt>yyyy-MM-dd'T'HH:mm:ss</tt>.
  76. */
  77. public static final FastDateFormat ISO_DATETIME_FORMAT
  78. = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss");
  79. /**
  80. * ISO8601 formatter for date-time with time zone.
  81. * The format used is <tt>yyyy-MM-dd'T'HH:mm:ssZZ</tt>.
  82. */
  83. public static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT
  84. = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZZ");
  85. /**
  86. * ISO8601 formatter for date without time zone.
  87. * The format used is <tt>yyyy-MM-dd</tt>.
  88. */
  89. public static final FastDateFormat ISO_DATE_FORMAT
  90. = FastDateFormat.getInstance("yyyy-MM-dd");
  91. /**
  92. * ISO8601-like formatter for date with time zone.
  93. * The format used is <tt>yyyy-MM-ddZZ</tt>.
  94. * This pattern does not comply with the formal ISO8601 specification
  95. * as the standard does not allow a time zone without a time.
  96. */
  97. public static final FastDateFormat ISO_DATE_TIME_ZONE_FORMAT
  98. = FastDateFormat.getInstance("yyyy-MM-ddZZ");
  99. /**
  100. * ISO8601 formatter for time without time zone.
  101. * The format used is <tt>'T'HH:mm:ss</tt>.
  102. */
  103. public static final FastDateFormat ISO_TIME_FORMAT
  104. = FastDateFormat.getInstance("'T'HH:mm:ss");
  105. /**
  106. * ISO8601 formatter for time with time zone.
  107. * The format used is <tt>'T'HH:mm:ssZZ</tt>.
  108. */
  109. public static final FastDateFormat ISO_TIME_TIME_ZONE_FORMAT
  110. = FastDateFormat.getInstance("'T'HH:mm:ssZZ");
  111. /**
  112. * ISO8601-like formatter for time without time zone.
  113. * The format used is <tt>HH:mm:ss</tt>.
  114. * This pattern does not comply with the formal ISO8601 specification
  115. * as the standard requires the 'T' prefix for times.
  116. */
  117. public static final FastDateFormat ISO_TIME_NO_T_FORMAT
  118. = FastDateFormat.getInstance("HH:mm:ss");
  119. /**
  120. * ISO8601-like formatter for time with time zone.
  121. * The format used is <tt>HH:mm:ssZZ</tt>.
  122. * This pattern does not comply with the formal ISO8601 specification
  123. * as the standard requires the 'T' prefix for times.
  124. */
  125. public static final FastDateFormat ISO_TIME_NO_T_TIME_ZONE_FORMAT
  126. = FastDateFormat.getInstance("HH:mm:ssZZ");
  127. /**
  128. * SMTP (and probably other) date headers.
  129. * The format used is <tt>EEE, dd MMM yyyy HH:mm:ss Z</tt> in US locale.
  130. */
  131. public static final FastDateFormat SMTP_DATETIME_FORMAT
  132. = FastDateFormat.getInstance("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
  133. //-----------------------------------------------------------------------
  134. /**
  135. * <p>DateFormatUtils instances should NOT be constructed in standard programming.</p>
  136. *
  137. * <p>This constructor is public to permit tools that require a JavaBean instance
  138. * to operate.</p>
  139. */
  140. public DateFormatUtils() {
  141. }
  142. /**
  143. * <p>Format a date/time into a specific pattern using the UTC time zone.</p>
  144. *
  145. * @param millis the date to format expressed in milliseconds
  146. * @param pattern the pattern to use to format the date
  147. * @return the formatted date
  148. */
  149. public static String formatUTC(long millis, String pattern) {
  150. return format(new Date(millis), pattern, DateUtils.UTC_TIME_ZONE, null);
  151. }
  152. /**
  153. * <p>Format a date/time into a specific pattern using the UTC time zone.</p>
  154. *
  155. * @param date the date to format
  156. * @param pattern the pattern to use to format the date
  157. * @return the formatted date
  158. */
  159. public static String formatUTC(Date date, String pattern) {
  160. return format(date, pattern, DateUtils.UTC_TIME_ZONE, null);
  161. }
  162. /**
  163. * <p>Format a date/time into a specific pattern using the UTC time zone.</p>
  164. *
  165. * @param millis the date to format expressed in milliseconds
  166. * @param pattern the pattern to use to format the date
  167. * @param locale the locale to use, may be <code>null</code>
  168. * @return the formatted date
  169. */
  170. public static String formatUTC(long millis, String pattern, Locale locale) {
  171. return format(new Date(millis), pattern, DateUtils.UTC_TIME_ZONE, locale);
  172. }
  173. /**
  174. * <p>Format a date/time into a specific pattern using the UTC time zone.</p>
  175. *
  176. * @param date the date to format
  177. * @param pattern the pattern to use to format the date
  178. * @param locale the locale to use, may be <code>null</code>
  179. * @return the formatted date
  180. */
  181. public static String formatUTC(Date date, String pattern, Locale locale) {
  182. return format(date, pattern, DateUtils.UTC_TIME_ZONE, locale);
  183. }
  184. /**
  185. * <p>Format a date/time into a specific pattern.</p>
  186. *
  187. * @param millis the date to format expressed in milliseconds
  188. * @param pattern the pattern to use to format the date
  189. * @return the formatted date
  190. */
  191. public static String format(long millis, String pattern) {
  192. return format(new Date(millis), pattern, null, null);
  193. }
  194. /**
  195. * <p>Format a date/time into a specific pattern.</p>
  196. *
  197. * @param date the date to format
  198. * @param pattern the pattern to use to format the date
  199. * @return the formatted date
  200. */
  201. public static String format(Date date, String pattern) {
  202. return format(date, pattern, null, null);
  203. }
  204. /**
  205. * <p>Format a date/time into a specific pattern in a time zone.</p>
  206. *
  207. * @param millis the time expressed in milliseconds
  208. * @param pattern the pattern to use to format the date
  209. * @param timeZone the time zone to use, may be <code>null</code>
  210. * @return the formatted date
  211. */
  212. public static String format(long millis, String pattern, TimeZone timeZone) {
  213. return format(new Date(millis), pattern, timeZone, null);
  214. }
  215. /**
  216. * <p>Format a date/time into a specific pattern in a time zone.</p>
  217. *
  218. * @param date the date to format
  219. * @param pattern the pattern to use to format the date
  220. * @param timeZone the time zone to use, may be <code>null</code>
  221. * @return the formatted date
  222. */
  223. public static String format(Date date, String pattern, TimeZone timeZone) {
  224. return format(date, pattern, timeZone, null);
  225. }
  226. /**
  227. * <p>Format a date/time into a specific pattern in a locale.</p>
  228. *
  229. * @param millis the date to format expressed in milliseconds
  230. * @param pattern the pattern to use to format the date
  231. * @param locale the locale to use, may be <code>null</code>
  232. * @return the formatted date
  233. */
  234. public static String format(long millis, String pattern, Locale locale) {
  235. return format(new Date(millis), pattern, null, locale);
  236. }
  237. /**
  238. * <p>Format a date/time into a specific pattern in a locale.</p>
  239. *
  240. * @param date the date to format
  241. * @param pattern the pattern to use to format the date
  242. * @param locale the locale to use, may be <code>null</code>
  243. * @return the formatted date
  244. */
  245. public static String format(Date date, String pattern, Locale locale) {
  246. return format(date, pattern, null, locale);
  247. }
  248. /**
  249. * <p>Format a date/time into a specific pattern in a time zone and locale.</p>
  250. *
  251. * @param millis the date to format expressed in milliseconds
  252. * @param pattern the pattern to use to format the date
  253. * @param timeZone the time zone to use, may be <code>null</code>
  254. * @param locale the locale to use, may be <code>null</code>
  255. * @return the formatted date
  256. */
  257. public static String format(long millis, String pattern, TimeZone timeZone, Locale locale) {
  258. return format(new Date(millis), pattern, timeZone, locale);
  259. }
  260. /**
  261. * <p>Format a date/time into a specific pattern in a time zone and locale.</p>
  262. *
  263. * @param date the date to format
  264. * @param pattern the pattern to use to format the date
  265. * @param timeZone the time zone to use, may be <code>null</code>
  266. * @param locale the locale to use, may be <code>null</code>
  267. * @return the formatted date
  268. */
  269. public static String format(Date date, String pattern, TimeZone timeZone, Locale locale) {
  270. FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone, locale);
  271. return df.format(date);
  272. }
  273. }