View Javadoc

1   /* ------------------------------------
2    * Kaamelot - 2006
3    * ------------------------------------
4    * Projet  : projectName
5    * Fichier : DateTools.java
6    * $Id$ 
7    * $Date$ 
8    * $Log$
9    * 
10   */
11  package com.atlassian.jira.util;
12  
13  import java.text.DecimalFormat;
14  import java.text.NumberFormat;
15  import java.util.Calendar;
16  import java.util.Date;
17  
18  import com.atlassian.core.util.DateUtils;
19  import com.atlassian.jira.ManagerFactory;
20  import com.atlassian.jira.config.properties.APKeys;
21  
22  import electric.xml.ParseException;
23  
24  /**
25   * Provide a set of Method for Date and Calendar
26   * 
27   * @author Kaamelot - V.Thoul� - 2006
28   * @version $Id$
29   * @history
30   *          <ul>
31   * @history - 01 Feb 2005 - Class Creation
32   */
33  public class DateTools {
34  
35  	/**
36  	 * Constants for Date Operations
37  	 */
38  	public final static String BOM = "BOM";
39  	public final static String BOQ = "BOQ";
40  	public final static String BOY = "BOY";
41  
42  	public final static String EOM = "EOM";
43  	public final static String EOQ = "EOQ";
44  	public final static String EOY = "EOY";
45  
46  	/**
47  	 * Constants for time computation
48  	 */
49  	public final static int SECOND = 1;
50  
51  	public final static int MINUTE = 60 * SECOND;
52  
53  	public final static int HOUR = 60 * MINUTE;
54  
55  	public final static int DAY = 24 * HOUR;
56  
57  	public final static int WEEK = 7 * DAY;
58  
59  	public final static int MONTH = 31 * DAY;
60  
61  	public static final long SECOND_MILLIS = 1000L * SECOND;
62  
63  	public static final long MINUTE_MILLIS = 1000L * MINUTE;
64  
65  	public static final long HOUR_MILLIS = 1000L * HOUR;
66  
67  	public static final long DAY_MILLIS = 1000L * DAY;
68  
69  	public static final long MONTH_MILLIS = 1000L * MONTH;
70  	public static final NumberFormat TWO_DIGITS = new DecimalFormat("00");
71  	private DecimalFormat decimalFormat = new DecimalFormat("0.##");
72  
73  	public static Calendar getBeginOfPeriod(final Date _date, final int _period) {
74  		Calendar calendar = Calendar.getInstance();
75  		calendar.setTime(_date);
76  		calendar.set(_period, calendar.getActualMinimum(_period));
77  		return calendar;
78  	}
79  
80  	public static Calendar getEndOfPeriod(final Date _date, final int _period) {
81  		Calendar calendar = Calendar.getInstance();
82  		calendar.setTime(_date);
83  		calendar.set(_period, calendar.getActualMaximum(_period));
84  		return calendar;
85  	}
86  
87  	public static Calendar getBeginOfMonth(final Date _date) {
88  		Calendar cal = getBeginOfPeriod(_date, Calendar.DAY_OF_MONTH);
89  		cal.clear(Calendar.MILLISECOND);
90  		cal.clear(Calendar.MINUTE);
91  		cal.clear(Calendar.SECOND);
92  		cal.clear(Calendar.HOUR);
93  		cal.clear(Calendar.HOUR_OF_DAY);
94  		// cal.getTimeInMillis();
95  		return cal;
96  	}
97  
98  	public static Calendar getEndOfMonth(final Date _date) {
99  		Calendar cal = getEndOfPeriod(_date, Calendar.DAY_OF_MONTH);
100 		cal.clear(Calendar.MILLISECOND);
101 		cal.clear(Calendar.MINUTE);
102 		cal.clear(Calendar.SECOND);
103 		cal.clear(Calendar.HOUR);
104 		cal.clear(Calendar.HOUR_OF_DAY);
105 		// cal.getTimeInMillis();
106 		return cal;
107 	}
108 
109 	public static Calendar getBeginOfWeek(final Date _date) {
110 		return getBeginOfPeriod(_date, Calendar.DAY_OF_WEEK);
111 	}
112 
113 	public static Calendar getEndOfWeek(final Date _date) {
114 		return getEndOfPeriod(_date, Calendar.DAY_OF_WEEK);
115 	}
116 
117 	public static Calendar getBeginOfYear(final Date _date) {
118 		Calendar cal = getBeginOfPeriod(_date, Calendar.DAY_OF_YEAR);
119 		cal.clear(Calendar.MILLISECOND);
120 		cal.clear(Calendar.MINUTE);
121 		cal.clear(Calendar.SECOND);
122 		cal.clear(Calendar.HOUR);
123 		cal.clear(Calendar.HOUR_OF_DAY);
124 		// cal.getTimeInMillis();
125 		return cal;
126 	}
127 
128 	public static Calendar getEndOfYear(final Date _date) {
129 		return getEndOfPeriod(_date, Calendar.DAY_OF_YEAR);
130 	}
131 
132 	/**
133 	 * @return Today Date with hour set to 00:00:00.0
134 	 * @throws ParseException
135 	 */
136 	public static Date getToday()  {
137 		// Date today = new Date();
138 		Calendar cal = Calendar.getInstance();
139 		cal.setTime(new Date());
140 		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
141 		return cal.getTime();
142 	}
143 
144 	public static Integer getDayOfMonth(final Object _date) {
145 		Date date = (Date) _date;
146 		Calendar calendarDate = Calendar.getInstance();
147 		calendarDate.setTime(date);
148 		return new Integer(calendarDate.get(Calendar.DAY_OF_MONTH));
149 	}
150 
151 	public static Calendar getEndOfDay(final Date _date) {
152 		Calendar calendar = Calendar.getInstance();
153 		calendar.setTime(_date);
154 		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
155 		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
156 		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
157 		calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
158 
159 		return calendar;
160 	}
161 
162 	public static Calendar getBeginOfDay(final Date _date) {
163 		Calendar calendar = Calendar.getInstance();
164 		calendar.setTime(_date);
165 		calendar.clear(Calendar.MILLISECOND);
166 		calendar.clear(Calendar.MINUTE);
167 		calendar.clear(Calendar.SECOND);
168 		calendar.clear(Calendar.HOUR);
169 		calendar.clear(Calendar.HOUR_OF_DAY);
170 		return calendar;
171 	}
172 
173 	/**
174 	 * @return Nb hours assumes in a Day for JIRA Timetracking
175 	 */
176 	public static int getHoursPerDay() {
177 		int hoursPerDay = new Integer(ManagerFactory.getApplicationProperties().getDefaultBackedString(APKeys.JIRA_TIMETRACKING_HOURS_PER_DAY)).intValue();
178 		return hoursPerDay;
179 	}
180 
181 	/**
182 	 * @return Nb days assumes in a Week for JIRA Timetracking
183 	 */
184 	public static int getDaysPerWeek() {
185 		int daysPerWeek = new Integer(ManagerFactory.getApplicationProperties().getDefaultBackedString(APKeys.JIRA_TIMETRACKING_DAYS_PER_WEEK)).intValue();
186 		return daysPerWeek;
187 	}
188 
189 	// /**
190 	// * @param locale
191 	// * @return
192 	// */
193 	// private static ResourceBundle getDefaultResourceBundle(final Locale
194 	// locale) {
195 	// ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
196 	// return ResourceBundle.getBundle(JiraWebActionSupport.class.getName(),
197 	// locale, classLoader);
198 	// }
199 	//
200 	// /**
201 	// * @param duration
202 	// * @param locale
203 	// * @return
204 	// */
205 	// public static String getPrettyDuration(final Long duration, final Locale
206 	// locale) {
207 	// int hoursPerDay =
208 	// new
209 	// Integer(ManagerFactory.getApplicationProperties().getDefaultBackedString(APKeys.JIRA_TIMETRACKING_HOURS_PER_DAY)).intValue();
210 	// int daysPerWeek =
211 	// new
212 	// Integer(ManagerFactory.getApplicationProperties().getDefaultBackedString(APKeys.JIRA_TIMETRACKING_DAYS_PER_WEEK)).intValue();
213 	//
214 	// return DateUtils.getDurationPretty(duration.longValue(), hoursPerDay,
215 	// daysPerWeek, getDefaultResourceBundle(locale));
216 	// }
217 
218 	public static String getNiceTimeDuration(long duration) {
219 		int durationDays = 0;
220 		double durationRemaingDayFraction = 0;
221 		double durationDayFraction = 0;
222 		int secondInDay = 8 * 60 * 60;
223 		String durationDayFractionStr = "";
224 		if (duration != 0) {
225 			durationDays = (int) (duration / secondInDay);
226 			durationRemaingDayFraction = (double) (duration - (durationDays * secondInDay)) / secondInDay;
227 			durationDayFraction = (double) durationRemaingDayFraction + durationDays;
228 			durationDayFractionStr = (new DecimalFormat("#.###")).format(durationDayFraction);
229 		}
230 		return durationDayFractionStr;
231 	}
232 
233 	public static String getDurationString(final long _duration) {
234 		boolean isNegative = (_duration<0);
235 		return (isNegative?"-":"") + DateUtils.getDurationString(Math.abs(_duration), getHoursPerDay(), getDaysPerWeek());
236 	}
237 
238 	public static String getDurationShortString(final long _duration) {
239 		return getDurationShortString(_duration, getHoursPerDay(), getDaysPerWeek());
240 	}
241 
242 	public static String getDurationShortString(long l, int hoursPerDay, int daysPerWeek) {
243 		int secondsInDay = hoursPerDay * HOUR;
244 		int secondsPerWeek = daysPerWeek * secondsInDay;
245 		return getDurationShortStringSeconds(l, secondsInDay, secondsPerWeek);
246 	}
247 
248 	public static String getDurationShortStringSeconds(long duration, int secondsPerDay, int secondsPerWeek) {
249 		long l = duration;
250 
251 		StringBuffer result = new StringBuffer();
252 
253 		if (l >= secondsPerWeek) {
254 			result.append((l / secondsPerWeek));
255 			result.append("w.");
256 			l = l % secondsPerWeek;
257 			//if (l > secondsPerDay)
258 		//		result.append("w.");
259 		}
260 
261 		if (l >= secondsPerDay) {
262 			result.append((l / secondsPerDay));
263 			result.append("d.-");
264 			// result.append("d ");
265 			l = l % secondsPerDay;
266 			// if (l>0) result.append("-");
267 		}
268 
269 		long t = 0;
270 		if (l >= HOUR) {
271 			t = (l / HOUR);
272 			l = l % HOUR;
273 		} 
274 		result.append(TWO_DIGITS.format(t));
275 		result.append(":");
276 
277 		t = 0;
278 		if (l >= MINUTE) {
279 			t = (l / MINUTE);
280 			l = l % MINUTE;
281 		}
282 		result.append(TWO_DIGITS.format(t));
283 
284 		return result.toString().trim();
285 	}
286 	
287 	public static String getDurationWDHMS(final long _duration) {
288 		return getDurationWDHMS(_duration, DAY, WEEK);
289 	}
290 
291 	public static String getDurationWDHMS(long duration, int secondsPerDay, int secondsPerWeek) {
292 		long l = duration;
293 
294 		StringBuffer result = new StringBuffer();
295 
296 		if (l >= secondsPerWeek) {
297 			result.append((l / secondsPerWeek));
298 			result.append("w ");
299 			l = l % secondsPerWeek;
300 		}
301 
302 		if (l >= secondsPerDay) {
303 			result.append((l / secondsPerDay));
304 			result.append("d ");
305 			l = l % secondsPerDay;
306 		}
307 
308 		long t = 0;
309 		if (l >= HOUR) {
310 			result.append((l / HOUR));
311 			result.append("h ");
312 			l = l % HOUR;
313 		} 
314 
315 		t = 0;
316 		if (l >= MINUTE) {
317 			result.append((l / MINUTE));
318 			result.append("m ");
319 			l = l % MINUTE;
320 		}
321 
322 		t = 0;
323 		if (l >= SECOND) {
324 			result.append((l ));
325 			result.append("s");
326 //			l = l % SECOND;
327 		}
328 
329 		return result.toString().trim();
330 	}
331 	
332 	/**
333 	 * Format duration value in hours
334 	 * @param value
335 	 * @return value
336 	 */
337     public String getPrettyHours(long value)
338     {
339     	return getHours(value) + "h";
340     }
341 
342 	/**
343 	 * Format duration value in hours
344 	 * @param value
345 	 * @return pretty formatted value
346 	 */
347     public String getHours(long value)
348     {
349     	return decimalFormat.format(((float)value) / 60 / 60);
350     }
351     	
352 
353 	public static boolean isNegative(long _long) {
354 		return (_long<0);
355 	}
356 
357 	public static boolean isNegative(int _int) {
358 		return (_int<0);
359 	}
360 
361 }