PageRenderTime 81ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/rssowl-2.0.5/org.rssowl.core/src/org/rssowl/core/util/DateUtils.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 252 lines | 106 code | 50 blank | 96 comment | 28 complexity | 96a6b617a70d9c3b36a18fa12ab7816f MD5 | raw file
  1. /* ********************************************************************** **
  2. ** Copyright notice **
  3. ** **
  4. ** (c) 2005-2009 RSSOwl Development Team **
  5. ** http://www.rssowl.org/ **
  6. ** **
  7. ** All rights reserved **
  8. ** **
  9. ** This program and the accompanying materials are made available under **
  10. ** the terms of the Eclipse Public License v1.0 which accompanies this **
  11. ** distribution, and is available at: **
  12. ** http://www.rssowl.org/legal/epl-v10.html **
  13. ** **
  14. ** A copy is found in the file epl-v10.html and important notices to the **
  15. ** license from the team is found in the textfile LICENSE.txt distributed **
  16. ** in this package. **
  17. ** **
  18. ** This copyright notice MUST APPEAR in all copies of the file! **
  19. ** **
  20. ** Contributors: **
  21. ** RSSOwl Development Team - initial API and implementation **
  22. ** **
  23. ** ********************************************************************** */
  24. package org.rssowl.core.util;
  25. import org.eclipse.core.runtime.Assert;
  26. import org.rssowl.core.persist.INews;
  27. import java.text.DateFormat;
  28. import java.text.ParseException;
  29. import java.text.SimpleDateFormat;
  30. import java.util.Calendar;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.Locale;
  34. import java.util.TimeZone;
  35. /**
  36. * Utility Class for working with <code>Dates</code>.
  37. *
  38. * @author bpasero
  39. */
  40. public class DateUtils {
  41. /** 1 Day in Millis */
  42. public static final long DAY = 24L * 60L * 60L * 1000L;
  43. /** 1 Week in Millis */
  44. public static final long WEEK = 7 * DAY;
  45. /* An array of custom date formats */
  46. private static final DateFormat[] CUSTOM_DATE_FORMATS;
  47. /* The Default Timezone to be used */
  48. private static final TimeZone TIMEZONE = TimeZone.getTimeZone("UTC"); //$NON-NLS-1$
  49. /**
  50. * @return A Calendar instance with the time being Today with a Time of
  51. * 0:00:00
  52. */
  53. public static Calendar getToday() {
  54. Calendar today = Calendar.getInstance();
  55. today.set(Calendar.HOUR_OF_DAY, 0);
  56. today.set(Calendar.MINUTE, 0);
  57. today.set(Calendar.SECOND, 0);
  58. today.set(Calendar.MILLISECOND, 0);
  59. return today;
  60. }
  61. /**
  62. * Returns the first Date-Field from the given News that is not NULL. Tries
  63. * Modified-Date, Publish-Date, and Received-Date. The latter one is never
  64. * NULL so this Method will never return NULL at all.
  65. *
  66. * @param news The News to get the Date from.
  67. * @return Either Modified-Date, Publish-Date or Received-Date if the formers
  68. * are NULL.
  69. */
  70. public static Date getRecentDate(INews news) {
  71. if (news.getModifiedDate() != null)
  72. return news.getModifiedDate();
  73. if (news.getPublishDate() != null)
  74. return news.getPublishDate();
  75. return news.getReceiveDate();
  76. }
  77. /**
  78. * Works like getRecentData(INews news) with the difference of returning the
  79. * most recent date from a List of News.
  80. *
  81. * @param news A List of News to get the most recent Date from.
  82. * @return Either Modified-Date, Publish-Date or Received-Date from the most
  83. * recent News.
  84. */
  85. public static Date getRecentDate(List<INews> news) {
  86. Assert.isTrue(!news.isEmpty());
  87. Date mostRecentDate = null;
  88. for (INews newsitem : news) {
  89. Date date = getRecentDate(newsitem);
  90. if (mostRecentDate == null || date.after(mostRecentDate))
  91. mostRecentDate = date;
  92. }
  93. return mostRecentDate;
  94. }
  95. /**
  96. * Tries different date formats to parse against the given string
  97. * representation to retrieve a valid Date object.
  98. *
  99. * @param strdate Date as String
  100. * @return Date The parsed Date
  101. */
  102. public static Date parseDate(String strdate) {
  103. /* Return in case the string date is not set */
  104. if (strdate == null || strdate.length() == 0)
  105. return null;
  106. Date result = null;
  107. strdate = strdate.trim();
  108. if (strdate.length() > 10) {
  109. /* Open: deal with +4:00 (no zero before hour) */
  110. if ((strdate.substring(strdate.length() - 5).indexOf("+") == 0 || strdate.substring(strdate.length() - 5).indexOf("-") == 0) && strdate.substring(strdate.length() - 5).indexOf(":") == 2) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  111. String sign = strdate.substring(strdate.length() - 5, strdate.length() - 4);
  112. strdate = strdate.substring(0, strdate.length() - 5) + sign + "0" + strdate.substring(strdate.length() - 4); //$NON-NLS-1$
  113. }
  114. String dateEnd = strdate.substring(strdate.length() - 6);
  115. /*
  116. * try to deal with -05:00 or +02:00 at end of date replace with -0500 or
  117. * +0200
  118. */
  119. if ((dateEnd.indexOf("-") == 0 || dateEnd.indexOf("+") == 0) && dateEnd.indexOf(":") == 3) { //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
  120. if (!"GMT".equals(strdate.substring(strdate.length() - 9, strdate.length() - 6))) { //$NON-NLS-1$
  121. String oldDate = strdate;
  122. String newEnd = dateEnd.substring(0, 3) + dateEnd.substring(4);
  123. strdate = oldDate.substring(0, oldDate.length() - 6) + newEnd;
  124. }
  125. }
  126. }
  127. /* Try to parse the date */
  128. int i = 0;
  129. while (i < CUSTOM_DATE_FORMATS.length) {
  130. try {
  131. /*
  132. * This Block needs to be synchronized, because the parse-Method in
  133. * SimpleDateFormat is not Thread-Safe.
  134. */
  135. synchronized (CUSTOM_DATE_FORMATS[i]) {
  136. return CUSTOM_DATE_FORMATS[i].parse(strdate);
  137. }
  138. } catch (ParseException e) {
  139. i++;
  140. } catch (NumberFormatException e) {
  141. i++;
  142. }
  143. }
  144. return result;
  145. }
  146. /** Initialize the array of common date formats and formatter */
  147. static {
  148. /* Create Date Formats */
  149. final String[] possibleDateFormats = {
  150. /* RFC 1123 with 2-digit Year */
  151. "EEE, dd MMM yy HH:mm:ss z", //$NON-NLS-1$
  152. /* RFC 1123 with 4-digit Year */
  153. "EEE, dd MMM yyyy HH:mm:ss z", //$NON-NLS-1$
  154. /* RFC 1123 with no Timezone */
  155. "EEE, dd MMM yy HH:mm:ss", //$NON-NLS-1$
  156. /* Variant of RFC 1123 */
  157. "EEE, MMM dd yy HH:mm:ss", //$NON-NLS-1$
  158. /* RFC 1123 with no Seconds */
  159. "EEE, dd MMM yy HH:mm z", //$NON-NLS-1$
  160. /* Variant of RFC 1123 */
  161. "EEE dd MMM yyyy HH:mm:ss", //$NON-NLS-1$
  162. /* RFC 1123 with no Day */
  163. "dd MMM yy HH:mm:ss z", //$NON-NLS-1$
  164. /* RFC 1123 with no Day or Seconds */
  165. "dd MMM yy HH:mm z", //$NON-NLS-1$
  166. /* ISO 8601 slightly modified */
  167. "yyyy-MM-dd'T'HH:mm:ssZ", //$NON-NLS-1$
  168. /* ISO 8601 slightly modified */
  169. "yyyy-MM-dd'T'HH:mm:ss'Z'", //$NON-NLS-1$
  170. /* ISO 8601 slightly modified */
  171. "yyyy-MM-dd'T'HH:mm:sszzzz", //$NON-NLS-1$
  172. /* ISO 8601 slightly modified */
  173. "yyyy-MM-dd'T'HH:mm:ss z", //$NON-NLS-1$
  174. /* ISO 8601 */
  175. "yyyy-MM-dd'T'HH:mm:ssz", //$NON-NLS-1$
  176. /* ISO 8601 slightly modified */
  177. "yyyy-MM-dd'T'HH:mm:ss.SSSz", //$NON-NLS-1$
  178. /* ISO 8601 slightly modified */
  179. "yyyy-MM-dd'T'HHmmss.SSSz", //$NON-NLS-1$
  180. /* ISO 8601 slightly modified */
  181. "yyyy-MM-dd'T'HH:mm:ss", //$NON-NLS-1$
  182. /* ISO 8601 w/o seconds */
  183. "yyyy-MM-dd'T'HH:mmZ", //$NON-NLS-1$
  184. /* ISO 8601 w/o seconds */
  185. "yyyy-MM-dd'T'HH:mm'Z'", //$NON-NLS-1$
  186. /* RFC 1123 without Day Name */
  187. "dd MMM yyyy HH:mm:ss z", //$NON-NLS-1$
  188. /* RFC 1123 without Day Name and Seconds */
  189. "dd MMM yyyy HH:mm z", //$NON-NLS-1$
  190. /* Simple Date Format */
  191. "yyyy-MM-dd", //$NON-NLS-1$
  192. /* Simple Date Format */
  193. "MMM dd, yyyy" //$NON-NLS-1$
  194. };
  195. /* Create the dateformats */
  196. CUSTOM_DATE_FORMATS = new SimpleDateFormat[possibleDateFormats.length];
  197. for (int i = 0; i < possibleDateFormats.length; i++) {
  198. CUSTOM_DATE_FORMATS[i] = new SimpleDateFormat(possibleDateFormats[i], Locale.ENGLISH);
  199. CUSTOM_DATE_FORMATS[i].setTimeZone(TIMEZONE);
  200. }
  201. }
  202. }