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

DateUtil.java 3.7KB

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