/
TimeUtil.java
63 lines (55 loc) · 2.07 KB
/
TimeUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package org.vsegda.util;
import java.text.SimpleDateFormat;
import java.text.ParsePosition;
import java.util.Date;
import java.util.TimeZone;
/**
* @author Roman Elizarov
*/
public class TimeUtil {
public static final TimeZone TIMEZONE = TimeZone.getTimeZone("Europe/Moscow");
public static final long SECOND = 1000L;
public static final long MINUTE = 60 * SECOND;
public static final long HOUR = 60 * MINUTE;
public static final long DAY = 24 * HOUR;
private static SimpleDateFormat getDateTimeFormat() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSS");
format.setTimeZone(TIMEZONE);
return format;
}
public static String formatDateTime(long timeMillis) {
return getDateTimeFormat().format(new Date(timeMillis));
}
public static String formatDateTimeDifference(long timeMillis, long now) {
StringBuilder sb = new StringBuilder();
long diff = now - timeMillis;
if (diff < 0) {
sb.append("-");
diff = -diff;
}
if (diff < HOUR)
sb.append(String.format("%dm%02ds", diff / MINUTE, (diff % MINUTE) / SECOND));
else if (diff < DAY)
sb.append(String.format("%dh%02dm", diff / HOUR, (diff % HOUR) / MINUTE));
else
sb.append(String.format("%dd%02dh", diff / DAY, (diff % DAY) / HOUR));
return sb.toString();
}
public static long parseTime(String s, long now) {
if (s.isEmpty())
return now;
ParsePosition pos = new ParsePosition(0);
Date date = getDateTimeFormat().parse(s, pos);
if (date != null && pos.getIndex() == s.length())
return date.getTime();
try {
return now + Long.parseLong(s);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid time format: " + s, e);
}
}
public static String getFormatClass(long timeMillis, long now) {
// Check if older than 15 mins ago
return timeMillis < now - 15 * 60000L ? "old" : "recent";
}
}