You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateUtils.java 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002 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 acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", 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 Group.
  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.tools.ant.util;
  55. import java.text.ChoiceFormat;
  56. import java.text.DateFormat;
  57. import java.text.MessageFormat;
  58. import java.text.SimpleDateFormat;
  59. import java.util.Calendar;
  60. import java.util.Date;
  61. import java.util.TimeZone;
  62. /**
  63. * Helper methods to deal with date/time formatting with a specific
  64. * defined format (<a href="http://www.w3.org/TR/NOTE-datetime">ISO8601</a>)
  65. * or a plurialization correct elapsed time in minutes and seconds.
  66. *
  67. * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
  68. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  69. *
  70. * @since Ant 1.5
  71. *
  72. * @version $Revision$
  73. */
  74. public final class DateUtils {
  75. /**
  76. * ISO8601-like pattern for date-time. It does not support timezone.
  77. * <tt>yyyy-MM-ddTHH:mm:ss</tt>
  78. */
  79. public final static String ISO8601_DATETIME_PATTERN
  80. = "yyyy-MM-dd'T'HH:mm:ss";
  81. /**
  82. * ISO8601-like pattern for date. <tt>yyyy-MM-dd</tt>
  83. */
  84. public final static String ISO8601_DATE_PATTERN
  85. = "yyyy-MM-dd";
  86. /**
  87. * ISO8601-like pattern for time. <tt>HH:mm:ss</tt>
  88. */
  89. public final static String ISO8601_TIME_PATTERN
  90. = "HH:mm:ss";
  91. // code from Magesh moved from DefaultLogger and slightly modified
  92. private final static MessageFormat MINUTE_SECONDS
  93. = new MessageFormat("{0}{1}");
  94. private final static double[] LIMITS = {0, 1, 2};
  95. private final static String[] MINUTES_PART =
  96. {"", "1 minute ", "{0,number} minutes "};
  97. private final static String[] SECONDS_PART =
  98. {"0 seconds", "1 second", "{1,number} seconds"};
  99. private final static ChoiceFormat MINUTES_FORMAT =
  100. new ChoiceFormat(LIMITS, MINUTES_PART);
  101. private final static ChoiceFormat SECONDS_FORMAT =
  102. new ChoiceFormat(LIMITS, SECONDS_PART);
  103. static {
  104. MINUTE_SECONDS.setFormat(0, MINUTES_FORMAT);
  105. MINUTE_SECONDS.setFormat(1, SECONDS_FORMAT);
  106. }
  107. /** private constructor */
  108. private DateUtils() {
  109. }
  110. /**
  111. * Format a date/time into a specific pattern.
  112. * @param date the date to format expressed in milliseconds.
  113. * @param pattern the pattern to use to format the date.
  114. * @return the formatted date.
  115. */
  116. public static String format(long date, String pattern) {
  117. return format(new Date(date), pattern);
  118. }
  119. /**
  120. * Format a date/time into a specific pattern.
  121. * @param date the date to format expressed in milliseconds.
  122. * @param pattern the pattern to use to format the date.
  123. * @return the formatted date.
  124. */
  125. public static String format(Date date, String pattern) {
  126. DateFormat df = createDateFormat(pattern);
  127. return df.format(date);
  128. }
  129. /**
  130. * Format an elapsed time into a plurialization correct string.
  131. * It is limited only to report elapsed time in minutes and
  132. * seconds and has the following behavior.
  133. * <ul>
  134. * <li>minutes are not displayed when 0. (ie: "45 seconds")</li>
  135. * <li>seconds are always displayed in plural form (ie "0 seconds" or
  136. * "10 seconds") except for 1 (ie "1 second")</li>
  137. * </ul>
  138. * @param time the elapsed time to report in milliseconds.
  139. * @return the formatted text in minutes/seconds.
  140. */
  141. public static String formatElapsedTime(long millis) {
  142. long seconds = millis / 1000;
  143. long minutes = seconds / 60;
  144. Object[] args = {new Long(minutes), new Long(seconds % 60)};
  145. return MINUTE_SECONDS.format(args);
  146. }
  147. /**
  148. * return a lenient date format set to GMT time zone.
  149. * @param pattern the pattern used for date/time formatting.
  150. * @return the configured format for this pattern.
  151. */
  152. private static DateFormat createDateFormat(String pattern) {
  153. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  154. TimeZone gmt = TimeZone.getTimeZone("GMT");
  155. sdf.setTimeZone(gmt);
  156. sdf.setLenient(true);
  157. return sdf;
  158. }
  159. /**
  160. * Calculate the phase of the moon for a given date.
  161. *
  162. * <p>Code heavily influenced by hacklib.c in <a
  163. * href="http://www.nethack.org/">Nethack</a></p>
  164. *
  165. * <p>The Algorithm:
  166. *
  167. * <pre>
  168. * moon period = 29.53058 days ~= 30, year = 365.2422 days
  169. *
  170. * days moon phase advances on first day of year compared to preceding year
  171. * = 365.2422 - 12*29.53058 ~= 11
  172. *
  173. * years in Metonic cycle (time until same phases fall on the same days of
  174. * the month) = 18.6 ~= 19
  175. *
  176. * moon phase on first day of year (epact) ~= (11*(year%19) + 18) % 30
  177. * (18 as initial condition for 1900)
  178. *
  179. * current phase in days = first day phase + days elapsed in year
  180. *
  181. * 6 moons ~= 177 days
  182. * 177 ~= 8 reported phases * 22
  183. * + 11/22 for rounding
  184. * </pre>
  185. *
  186. * @return The phase of the moon as a number between 0 and 7 with
  187. * 0 meaning new moon and 4 meaning full moon.
  188. *
  189. * @since 1.2, Ant 1.5
  190. */
  191. public static int getPhaseOfMoon(Calendar cal) {
  192. int dayOfTheYear = cal.get(Calendar.DAY_OF_YEAR);
  193. int yearInMetonicCycle = ((cal.get(Calendar.YEAR)-1900) % 19) + 1;
  194. int epact = (11 * yearInMetonicCycle + 18) % 30;
  195. if ((epact == 25 && yearInMetonicCycle > 11) || epact == 24) {
  196. epact++;
  197. }
  198. return (((((dayOfTheYear + epact) * 6) + 11) % 177) / 22) & 7;
  199. }
  200. }