1 package org.apache.archiva.redback.integration.util;
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.text.SimpleDateFormat;
23 import java.util.Calendar;
24 import java.util.Date;
25
26 /**
27 * DateUtils
28 *
29 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
30 *
31 */
32 public class DateUtils
33 {
34 /**
35 * Provided a date you will get a timestamp and the age/old that you want
36 *
37 * @param date the date to compare to now.
38 * @param suffix the suffix in the age string. using "ago" here would result in "2006 Aug 23, 11:43 pm - 12 days ago"
39 * @return the formated string.
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 * Provided a date you will get a timestamp and the age/old that you want.
48 *
49 * @param date the date to compare to now.
50 * @param dateFormat the {@link SimpleDateFormat} format string to use for the date.
51 * @param suffix the suffix in the age string. using "ago" here would result in "2006 Aug 23, 11:43 pm - 12 days ago"
52 * @return the formated string.
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 }