1 package org.codehaus.mojo.dashboard.report.plugin.utils;
2
3 /*
4 * Copyright 2007 David Vicente
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import java.util.Calendar;
20 import java.util.Date;
21 import java.util.Locale;
22 import java.util.Vector;
23
24 /**
25 *
26 * @author <a href="dvicente72@gmail.com">David Vicente</a>
27 *
28 */
29 public class DateUtils
30 {
31
32 /**
33 * Creation forbidden...
34 */
35 private DateUtils()
36 {
37 super();
38 }
39
40 /**
41 * return all dates between 2 dates defined by time Period
42 *
43 * @param startDate
44 * @param endDate
45 * @param timePeriod
46 * @return
47 * the Date array
48 */
49 public static Date[] getAllDates( Date startDate, Date endDate, TimePeriod timePeriod )
50 {
51 Vector dateList = new Vector();
52 Calendar cal = Calendar.getInstance( Locale.getDefault() );
53
54 cal.setTime( startDate );
55
56 Date tmpDate = startDate;
57
58 while ( tmpDate.before( endDate ) )
59 {
60 dateList.add( tmpDate );
61
62 if ( timePeriod.equals( TimePeriod.MINUTE ) )
63 {
64 cal.add( Calendar.MINUTE, 1 );
65 }
66 else if ( timePeriod.equals( TimePeriod.HOUR ) )
67 {
68 cal.add( Calendar.HOUR_OF_DAY, 1 );
69 }
70 else if ( timePeriod.equals( TimePeriod.DAY ) )
71 {
72 cal.add( Calendar.DATE, 1 );
73 }
74 else if ( timePeriod.equals( TimePeriod.WEEK ) )
75 {
76 cal.add( Calendar.DATE, 7 );
77 }
78 else if ( timePeriod.equals( TimePeriod.MONTH ) )
79 {
80 cal.add( Calendar.MONTH, 1 );
81 }
82 else
83 {
84 cal.add( Calendar.DATE, 1 );
85 }
86 tmpDate = cal.getTime();
87 }
88
89 dateList.add( endDate );
90
91 return (Date[]) dateList.toArray( new Date[dateList.size()] );
92 }
93 }