blob: 2fae918e3b2bf80f06a88d808b4a31cf8bd46792 (
plain) (
blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
package org.openslx.dozmod.util;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.openslx.bwlp.thrift.iface.UserInfo;
public class FormatHelper {
private static final DateTimeFormatter shortDateFormatter = DateTimeFormat.forPattern("dd.MM.yy HH:mm");
private static final DateTimeFormatter longDateFormatter = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss");
/**
* Convert unix timestamp to short date.
*
* @param timestamp unix time
* @return dd.MM.yy HH:mm
*/
public static String shortDate(long timestamp) {
if (timestamp == 0)
return "???";
return shortDateFormatter.print(timestamp * 1000l);
}
/**
* Convert unix timestamp to long date.
*
* @param timestamp unix time
* @return dd.MM.yyyy HH:mm:ss
*/
public static String longDate(long timestamp) {
if (timestamp == 0)
return "???";
return longDateFormatter.print(timestamp * 1000l);
}
/**
* Format the given user's name.
*
* @param user a {@link UserInfo} instance
* @return "LastName, FirstName"
*/
public static String userName(UserInfo user) {
if (user == null)
return "<null>";
return user.getLastName() + ", " + user.getFirstName();
}
/**
* Format bytes using suitable unit prefix.
*
* @param bytes unformatted byte count
* @param si true to use SI mode (1000B = 1KB), false for normal mode
* (1024B = 1KiB)
* @return Formatted string, i.e. <code>54.6 GiB</code>
*/
public static String byteToGigabyte(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit)
return bytes + "\u202FB";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPEZY").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f\u202F%sB", bytes / Math.pow(unit, exp), pre);
}
/**
* Parse the given String as a base10 integer.
* If the string does not represent a valid integer, return the given
* default value.
*
* @param value string representation to parse to an int
* @param defaultValue fallback value if given string can't be parsed
* @return
*/
public static int parseInt(String value, int defaultValue) {
try {
return Integer.parseInt(value);
} catch (Exception e) {
return defaultValue;
}
}
}
|