1 package org.apache.archiva.redback.integration.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.text.SimpleDateFormat;
23 import java.util.Calendar;
24 import java.util.Date;
25
26
27
28
29
30
31
32 public class DateUtils
33 {
34
35
36
37
38
39
40
41 public static String formatWithAge( Date date, String suffix )
42 {
43 return formatWithAge( date, "EEE, d MMM yyyy HH:mm:ss Z", suffix );
44 }
45
46
47
48
49
50
51
52
53
54 public static String formatWithAge( Date date, String dateFormat, String suffix )
55 {
56 if ( date == null )
57 {
58 return null;
59 }
60
61 SimpleDateFormat format = new SimpleDateFormat( dateFormat );
62
63 StringBuilder out = new StringBuilder();
64 out.append( format.format( date ) );
65 out.append( " - " );
66
67 Calendar now = Calendar.getInstance();
68 Calendar then = Calendar.getInstance();
69 then.setTime( date );
70
71 long diffMillis = now.getTimeInMillis() - then.getTimeInMillis();
72
73 long days = diffMillis / ( 24 * 60 * 60 * 1000 );
74 long hours = diffMillis / ( 60 * 60 * 1000 );
75 long minutes = diffMillis / ( 60 * 1000 );
76 long seconds = diffMillis / ( 1000 );
77
78 if ( days > 0 )
79 {
80 out.append( String.valueOf( days ) ).append( " day" );
81 if ( days > 1 )
82 {
83 out.append( 's' );
84 }
85 }
86 else if ( hours > 0 )
87 {
88 out.append( String.valueOf( hours ) ).append( " hour" );
89 if ( hours > 1 )
90 {
91 out.append( 's' );
92 }
93 }
94 else if ( minutes > 0 )
95 {
96 out.append( String.valueOf( minutes ) ).append( " minute" );
97 if ( minutes > 1 )
98 {
99 out.append( 's' );
100 }
101 }
102 else if ( seconds > 0 )
103 {
104 out.append( String.valueOf( seconds ) ).append( " second" );
105 if ( seconds > 1 )
106 {
107 out.append( 's' );
108 }
109 }
110
111 out.append( ' ' ).append( suffix );
112
113 return out.toString();
114 }
115 }