1package org.apache.archiva.redback.integration.util;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import java.text.SimpleDateFormat;
23import java.util.Calendar;
24import java.util.Date;
2526/**27 * DateUtils28 *29 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>30 *31 */32publicclassDateUtils33 {
34/**35 * Provided a date you will get a timestamp and the age/old that you want36 *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 */41publicstatic String formatWithAge( Date date, String suffix )
42 {
43return formatWithAge( date, "EEE, d MMM yyyy HH:mm:ss Z", suffix );
44 }
4546/**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 */54publicstatic String formatWithAge( Date date, String dateFormat, String suffix )
55 {
56if ( date == null )
57 {
58returnnull;
59 }
6061 SimpleDateFormat format = new SimpleDateFormat( dateFormat );
6263 StringBuilder out = new StringBuilder();
64 out.append( format.format( date ) );
65 out.append( " - " );
6667 Calendar now = Calendar.getInstance();
68 Calendar then = Calendar.getInstance();
69 then.setTime( date );
7071long diffMillis = now.getTimeInMillis() - then.getTimeInMillis();
7273long days = diffMillis / ( 24 * 60 * 60 * 1000 );
74long hours = diffMillis / ( 60 * 60 * 1000 );
75long minutes = diffMillis / ( 60 * 1000 );
76long seconds = diffMillis / ( 1000 );
7778if ( days > 0 )
79 {
80 out.append( String.valueOf( days ) ).append( " day" );
81if ( days > 1 )
82 {
83 out.append( 's' );
84 }
85 }
86elseif ( hours > 0 )
87 {
88 out.append( String.valueOf( hours ) ).append( " hour" );
89if ( hours > 1 )
90 {
91 out.append( 's' );
92 }
93 }
94elseif ( minutes > 0 )
95 {
96 out.append( String.valueOf( minutes ) ).append( " minute" );
97if ( minutes > 1 )
98 {
99 out.append( 's' );
100 }
101 }
102elseif ( seconds > 0 )
103 {
104 out.append( String.valueOf( seconds ) ).append( " second" );
105if ( seconds > 1 )
106 {
107 out.append( 's' );
108 }
109 }
110111 out.append( ' ' ).append( suffix );
112113return out.toString();
114 }
115 }