/WOGWT/Sources/wogwt/translatable/utils/DateUtils.java

http://wogwt.googlecode.com/ · Java · 230 lines · 76 code · 28 blank · 126 comment · 20 complexity · dfd1758696b3a11d964d5f5fdc81d946 MD5 · raw file

  1. /**
  2. * Copyright 2005-2009 Noelios Technologies.
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL 1.0 (the
  6. * "Licenses"). You can select the license that you prefer but you may not use
  7. * this file except in compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the LGPL 3.0 license at
  10. * http://www.opensource.org/licenses/lgpl-3.0.html
  11. *
  12. * You can obtain a copy of the LGPL 2.1 license at
  13. * http://www.opensource.org/licenses/lgpl-2.1.php
  14. *
  15. * You can obtain a copy of the CDDL 1.0 license at
  16. * http://www.opensource.org/licenses/cddl1.php
  17. *
  18. * You can obtain a copy of the EPL 1.0 license at
  19. * http://www.opensource.org/licenses/eclipse-1.0.php
  20. *
  21. * See the Licenses for the specific language governing permissions and
  22. * limitations under the Licenses.
  23. *
  24. * Alternatively, you can obtain a royalty free commercial license with less
  25. * limitations, transferable or non-transferable, directly at
  26. * http://www.noelios.com/products/restlet-engine
  27. *
  28. * Restlet is a registered trademark of Noelios Technologies.
  29. */
  30. package wogwt.translatable.utils;
  31. import java.util.Arrays;
  32. import java.util.Collections;
  33. import java.util.Date;
  34. import java.util.List;
  35. /**
  36. * WOGWT: copied from gwt-restlet to be used to parse and format
  37. * JSON dates in the format(s) required by ERRouteRequestHandler.
  38. *
  39. * Date manipulation utilities.
  40. *
  41. * @author Jerome Louvel
  42. */
  43. public final class DateUtils {
  44. /** Obsoleted HTTP date format (ANSI C asctime() format). */
  45. public static final List<String> FORMAT_ASC_TIME = unmodifiableList("EEE MMM dd HH:mm:ss yyyy");
  46. /** Obsoleted HTTP date format (RFC 1036). */
  47. public static final List<String> FORMAT_RFC_1036 = unmodifiableList("EEEE, dd-MMM-yy HH:mm:ss zzz");
  48. /** Preferred HTTP date format (RFC 1123). */
  49. public static final List<String> FORMAT_RFC_1123 = unmodifiableList("EEE, dd MMM yyyy HH:mm:ss zzz");
  50. /** W3C date format (RFC 3339). */
  51. public static final List<String> FORMAT_RFC_3339 = unmodifiableList("yyyy-MM-dd'T'HH:mm:ssz");
  52. /** Common date format (RFC 822). */
  53. public static final List<String> FORMAT_RFC_822 = unmodifiableList(
  54. "EEE, dd MMM yy HH:mm:ss z", "EEE, dd MMM yy HH:mm z",
  55. "dd MMM yy HH:mm:ss z", "dd MMM yy HH:mm z");
  56. /**
  57. * Compares two date with a precision of one second.
  58. *
  59. * @param baseDate
  60. * The base date
  61. * @param afterDate
  62. * The date supposed to be after.
  63. * @return True if the afterDate is indeed after the baseDate.
  64. */
  65. public static boolean after(final Date baseDate, final Date afterDate) {
  66. if ((baseDate == null) || (afterDate == null)) {
  67. throw new IllegalArgumentException(
  68. "Can't compare the dates, at least one of them is null");
  69. }
  70. final long baseTime = baseDate.getTime() / 1000;
  71. final long afterTime = afterDate.getTime() / 1000;
  72. return baseTime < afterTime;
  73. }
  74. /**
  75. * Compares two date with a precision of one second.
  76. *
  77. * @param baseDate
  78. * The base date
  79. * @param beforeDate
  80. * The date supposed to be before.
  81. * @return True if the beforeDate is indeed before the baseDate.
  82. */
  83. public static boolean before(final Date baseDate, final Date beforeDate) {
  84. if ((baseDate == null) || (beforeDate == null)) {
  85. throw new IllegalArgumentException(
  86. "Can't compare the dates, at least one of them is null");
  87. }
  88. final long baseTime = baseDate.getTime() / 1000;
  89. final long beforeTime = beforeDate.getTime() / 1000;
  90. return beforeTime < baseTime;
  91. }
  92. /**
  93. * Compares two date with a precision of one second.
  94. *
  95. * @param baseDate
  96. * The base date
  97. * @param otherDate
  98. * The other date supposed to be equals.
  99. * @return True if both dates are equals.
  100. */
  101. public static boolean equals(final Date baseDate, final Date otherDate) {
  102. if ((baseDate == null) || (otherDate == null)) {
  103. throw new IllegalArgumentException(
  104. "Can't compare the dates, at least one of them is null");
  105. }
  106. final long baseTime = baseDate.getTime() / 1000;
  107. final long otherTime = otherDate.getTime() / 1000;
  108. return otherTime == baseTime;
  109. }
  110. /**
  111. * Formats a Date in the default HTTP format (RFC 1123).
  112. *
  113. * @param date
  114. * The date to format.
  115. * @return The formatted date.
  116. */
  117. public static String format(final Date date) {
  118. return format(date, DateUtils.FORMAT_RFC_1123.get(0));
  119. }
  120. /**
  121. * Formats a Date according to the first format in the array.
  122. *
  123. * @param date
  124. * The date to format.
  125. * @param format
  126. * The date format to use.
  127. * @return The formatted date.
  128. */
  129. public static String format(final Date date, final String format) {
  130. if (date == null) {
  131. throw new IllegalArgumentException("Date is null");
  132. }
  133. /*
  134. * GWT difference: DateTimeFormat parser is is not passed a Locale in
  135. * the same way as SimpleDateFormat. It derives locale information from
  136. * the GWT application's locale.
  137. *
  138. * Default timezone is GMT unless specified via a GMT:hhmm, GMT:+hhmm,
  139. * or GMT:-hhmm string.
  140. */
  141. final com.google.gwt.i18n.client.DateTimeFormat formatter =
  142. com.google.gwt.i18n.client.DateTimeFormat.getFormat(format);
  143. return formatter.format(date);
  144. }
  145. /**
  146. * Parses a formatted date into a Date object.
  147. *
  148. * @param date
  149. * The date to parse.
  150. * @param formats
  151. * The date formats to use sorted by completeness.
  152. * @return The parsed date.
  153. */
  154. public static Date parse(final String date, final List<String> formats) {
  155. Date result = null;
  156. if (date == null) {
  157. throw new IllegalArgumentException("Date is null");
  158. }
  159. String format = null;
  160. int formatsSize = formats.size();
  161. for (int i = 0; (result == null) && (i < formatsSize); i++) {
  162. /*
  163. * GWT difference: DateTimeFormat parser is is not passed a Locale
  164. * in the same way as SimpleDateFormat. It derives locale
  165. * information from the GWT application's locale.
  166. *
  167. * Default timezone is GMT unless specified via a GMT:hhmm,
  168. * GMT:+hhmm, or GMT:-hhmm string.
  169. */
  170. final com.google.gwt.i18n.client.DateTimeFormat parser =
  171. com.google.gwt.i18n.client.DateTimeFormat.getFormat(format);
  172. try {
  173. result = parser.parse(date);
  174. } catch (Exception e) {
  175. // Ignores error as the next format may work better
  176. }
  177. }
  178. return result;
  179. }
  180. /**
  181. * Helper method to help initialize this class by providing unmodifiable
  182. * lists based on arrays.
  183. *
  184. * @param <T>
  185. * Any valid java object
  186. * @param array
  187. * to be convereted into an unmodifiable list
  188. * @return unmodifiable list based on the provided array
  189. */
  190. private static <T> List<T> unmodifiableList(final T... array) {
  191. return Collections.unmodifiableList(Arrays.asList(array));
  192. }
  193. public static native int timeZoneOffset() /*-{
  194. return new Date().getTimezoneOffset();
  195. }-*/;
  196. /**
  197. * Private constructor to ensure that the class acts as a true utility class
  198. * i.e. it isn't instatiable and extensible.
  199. */
  200. private DateUtils() {
  201. }
  202. }