1
2
3
4
5
6
7
8 package com.buckosoft.PicMan.util;
9
10 import java.text.DateFormat;
11 import java.util.Date;
12
13
14
15
16
17
18 public class DateUtil {
19 protected DateUtil() {}
20
21 public static final String s_colon = ":";
22
23
24
25
26
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))
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
66
67
68
69 public static String timeToString(long time) {
70 StringBuilder sb = new StringBuilder();
71 time = time /1000;
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 }