View Javadoc
1   /******************************************************************************
2    * DateUtil.java - Date utilities
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2007 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.util;
9   
10  import java.text.DateFormat;
11  import java.util.Date;
12  
13  /** Date utilities.
14   * @author Dick Balaska
15   * @since 2007/12/07
16   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/util/DateUtil.java">DateUtil.java</a>
17   */
18  public class DateUtil {
19  	protected DateUtil() {}		// Not an object that gets instantiated
20  
21  	public static final String s_colon = ":";
22  	
23  	///////////////////////////////////////////////////////////////////////////////
24  	/** Return the time passed as a string
25  	 * @param then The date to compare to
26  	 * @return "3 hours"
27  	 */
28  	public static String getTimeRelative(Date then)	{
29  		return(getTimeRelative(then, false));
30  	}
31  	
32  	public static String getTimeRelative(Date then, boolean past)	{
33  		String	ago = " ago";
34  		if (past)
35  			ago = "";
36  		StringBuffer	sb = new StringBuffer();
37  
38  		Date	rightnow = new Date();
39  		long	diff = (rightnow.getTime() - then.getTime())/1000;
40  		if (diff < 0)
41  		{
42  			diff = -diff;
43  			ago = "";
44  		}
45  		if (diff < 60)
46  			sb.append(diff + " second" + (diff == 1 ? "" : "s") + ago);
47  		else if (diff < (60*90))	// 1.5 hours
48  			sb.append(diff/60 + " minute" + ((diff/60 == 1) ? "" : "s") + ago);
49  		else if (diff < (60*60*50))
50  			sb.append(diff/(60*60) + " hour" + (diff/(60*60) == 1 ? "" : "s") + ago);
51  		else if (diff < (60*60*24*21))
52  			sb.append(diff/(60*60*24) + " day" +  (diff/(60*60*24)== 1 ? "" : "s") + ago);
53  		else if (diff < (60*60*24*7*5))
54  			sb.append(diff/(60*60*24*7) + " week" + (diff/(60*60*24*7) == 1 ? "" : "s") + ago);
55  		else
56  			sb.append(diff/(60*60*24*30) + " month" + (diff/(60*60*24*30) == 1 ? "" : "s") + ago);
57  		return(sb.toString());
58  	}
59  
60  	private static DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
61  	public static String getTimeHMS(Date when) {
62  		return(df.format(when));
63  	}
64  	
65  	/** Convert a long to a String like dd:hh:mm:ss.
66  	 * @param time Usually a diff between currentTime.getTime() and startTime.getTime().  Includes milliseconds.
67  	 * @return A String
68  	 */
69  	public static String timeToString(long time) {
70  		StringBuilder sb = new StringBuilder();
71  		time = time /1000;		// discard milliseconds
72  		int s = (int)(time % 60);
73  		int m = (int)((time / 60) % 60);
74  		int h = (int)((time / (60*60)) % (24));
75  		int d = (int)((time / (60*60*24)));
76  		if (s < 0 || m < 0 || h < 0 || d < 0)
77  			sb.append("-");
78  		s = Math.abs(s);
79  		m = Math.abs(m);
80  		h = Math.abs(h);
81  		d = Math.abs(d);
82  		if (d > 0) {
83  			sb.append(d);
84  			sb.append(":");
85  		}
86  		if (h < 10 &&  d > 0)
87  			sb.append("0");
88  		if (h > 0 || d > 0) {
89  			sb.append(h);
90  			sb.append(":");
91  		}
92  		if (m < 10)
93  			sb.append("0");
94  		sb.append(m);
95  		sb.append(":");
96  		if (s < 10)
97  			sb.append("0");
98  		sb.append(s);
99  		return(sb.toString());
100 	}
101 	
102 	
103 }