This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.common.utils;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Calendar;
023import java.util.Date;
024import java.util.GregorianCalendar;
025
026/**
027 * DateUtil - some (not-so) common date utility methods.
028 *
029 *
030 */
031public class DateUtil
032{
033    public static String getDuration( long duration )
034    {
035        return getDuration( new Date( 0 ), new Date( duration ) );
036    }
037
038    public static String getDuration( long ms1, long ms2 )
039    {
040        return getDuration( new Date( ms1 ), new Date( ms2 ) );
041    }
042
043    public static String getDuration( Date d1, Date d2 )
044    {
045        Calendar cal1 = new GregorianCalendar();
046        cal1.setTime( d1 );
047
048        Calendar cal2 = new GregorianCalendar();
049        cal2.setTime( d2 );
050
051        return getDuration( cal1, cal2 );
052    }
053
054    public static String getDuration( Calendar cal1, Calendar cal2 )
055    {
056        int year1 = cal1.get( Calendar.YEAR );
057        int day1 = cal1.get( Calendar.DAY_OF_YEAR );
058        int hour1 = cal1.get( Calendar.HOUR_OF_DAY );
059        int min1 = cal1.get( Calendar.MINUTE );
060        int sec1 = cal1.get( Calendar.SECOND );
061        int ms1 = cal1.get( Calendar.MILLISECOND );
062
063        int year2 = cal2.get( Calendar.YEAR );
064        int day2 = cal2.get( Calendar.DAY_OF_YEAR );
065        int hour2 = cal2.get( Calendar.HOUR_OF_DAY );
066        int min2 = cal2.get( Calendar.MINUTE );
067        int sec2 = cal2.get( Calendar.SECOND );
068        int ms2 = cal2.get( Calendar.MILLISECOND );
069
070        int leftDays = ( day1 - day2 ) + ( year1 - year2 ) * 365;
071        int leftHours = hour2 - hour1;
072        int leftMins = min2 - min1;
073        int leftSeconds = sec2 - sec1;
074        int leftMilliSeconds = ms2 - ms1;
075
076        if ( leftMilliSeconds < 0 )
077        {
078            leftMilliSeconds += 1000;
079            --leftSeconds;
080        }
081
082        if ( leftSeconds < 0 )
083        {
084            leftSeconds += 60;
085            --leftMins;
086        }
087
088        if ( leftMins < 0 )
089        {
090            leftMins += 60;
091            --leftHours;
092        }
093
094        if ( leftHours < 0 )
095        {
096            leftHours += 24;
097            --leftDays;
098        }
099
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}