This project has retired. For details please refer to its Attic page.
DateUtil xref
View Javadoc
1   package org.apache.archiva.common.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Calendar;
23  import java.util.Date;
24  import java.util.GregorianCalendar;
25  
26  /**
27   * DateUtil - some (not-so) common date utility methods.
28   *
29   *
30   */
31  public class DateUtil
32  {
33      public static String getDuration( long duration )
34      {
35          return getDuration( new Date( 0 ), new Date( duration ) );
36      }
37  
38      public static String getDuration( long ms1, long ms2 )
39      {
40          return getDuration( new Date( ms1 ), new Date( ms2 ) );
41      }
42  
43      public static String getDuration( Date d1, Date d2 )
44      {
45          Calendar cal1 = new GregorianCalendar();
46          cal1.setTime( d1 );
47  
48          Calendar cal2 = new GregorianCalendar();
49          cal2.setTime( d2 );
50  
51          return getDuration( cal1, cal2 );
52      }
53  
54      public static String getDuration( Calendar cal1, Calendar cal2 )
55      {
56          int year1 = cal1.get( Calendar.YEAR );
57          int day1 = cal1.get( Calendar.DAY_OF_YEAR );
58          int hour1 = cal1.get( Calendar.HOUR_OF_DAY );
59          int min1 = cal1.get( Calendar.MINUTE );
60          int sec1 = cal1.get( Calendar.SECOND );
61          int ms1 = cal1.get( Calendar.MILLISECOND );
62  
63          int year2 = cal2.get( Calendar.YEAR );
64          int day2 = cal2.get( Calendar.DAY_OF_YEAR );
65          int hour2 = cal2.get( Calendar.HOUR_OF_DAY );
66          int min2 = cal2.get( Calendar.MINUTE );
67          int sec2 = cal2.get( Calendar.SECOND );
68          int ms2 = cal2.get( Calendar.MILLISECOND );
69  
70          int leftDays = ( day1 - day2 ) + ( year1 - year2 ) * 365;
71          int leftHours = hour2 - hour1;
72          int leftMins = min2 - min1;
73          int leftSeconds = sec2 - sec1;
74          int leftMilliSeconds = ms2 - ms1;
75  
76          if ( leftMilliSeconds < 0 )
77          {
78              leftMilliSeconds += 1000;
79              --leftSeconds;
80          }
81  
82          if ( leftSeconds < 0 )
83          {
84              leftSeconds += 60;
85              --leftMins;
86          }
87  
88          if ( leftMins < 0 )
89          {
90              leftMins += 60;
91              --leftHours;
92          }
93  
94          if ( leftHours < 0 )
95          {
96              leftHours += 24;
97              --leftDays;
98          }
99  
100         StringBuilder interval = new StringBuilder();
101 
102         appendInterval( interval, leftDays, "Day" );
103         appendInterval( interval, leftHours, "Hour" );
104         appendInterval( interval, leftMins, "Minute" );
105         appendInterval( interval, leftSeconds, "Second" );
106         appendInterval( interval, leftMilliSeconds, "Millisecond" );
107 
108         return interval.toString();
109     }
110 
111     private static void appendInterval( StringBuilder interval, int count, String type )
112     {
113         if ( count > 0 )
114         {
115             if ( interval.length() > 0 )
116             {
117                 interval.append( " " );
118             }
119 
120             interval.append( count );
121             interval.append( " " ).append( type );
122             if ( count > 1 )
123             {
124                 interval.append( "s" );
125             }
126         }
127     }
128 
129 }