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
|
/**
* Copyright 2013 JogAmp Community. All rights reserved.
*
* This software is licensed under the GNU General Public License (GPL) Version 3.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package org.jogamp.jabot.util;
import java.util.Calendar;
import java.util.TimeZone;
public class TimeTool {
public static String UTC_TZ = "UTC";
public static String[] ZERO_TZs = { UTC_TZ, "GMT", "GMT0", "Zulu" };
/** Returns closest to Zulu/UTC TimeZone w/ shortest ID */
public static TimeZone getNearZuluTimeZone() {
TimeZone tz = null;
// Get preferred ..
for(int i=0; null == tz && i<ZERO_TZs.length; i++) {
tz = TimeZone.getTimeZone(ZERO_TZs[i]);
}
if(null == tz) {
// Get shortest ..
final String[] ids = TimeZone.getAvailableIDs(0);
if( null != ids && ids.length > 0 ) {
int min_len = Integer.MAX_VALUE;
int min_idx = -1;
for(int i=0; i<ids.length; i++) {
final int len = ids[i].length();
if( len < min_len ) {
min_len = len;
min_idx = i;
}
}
tz = TimeZone.getTimeZone(ids[min_idx]);
}
}
if(null == tz) {
// last resort - default
tz = TimeZone.getDefault();
}
return tz;
}
/**
* Returns timestamp of internal Calendar: YYYYMMDD HH:MM:SS
*
* @param calendar Calendar providing current time
* @param withHour if true includes daytime w/ or w/o spacing, otherwise just YYYYMMDD
* @param withSpacing if true provides spacing, otherwise just YYYYMMDDHHMMSS
* @return
*/
public static String getTimeStamp(final Calendar calendar, boolean withHour, boolean withSpacing) {
final int year, month, day, hour, minute, seconds;
{
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1; // Jan - 1
day = calendar.get(Calendar.DAY_OF_MONTH);
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
seconds = calendar.get(Calendar.SECOND);
}
if( !withHour ) {
return String.format("%04d%02d%02d", year, month, day);
} else {
if( !withSpacing ) {
return String.format("%04d%02d%02d%02d%02d%02d",
year, month, day, hour, minute, seconds);
} else {
return String.format("%04d%02d%02d %02d:%02d:%02d", year, month, day, hour, minute, seconds);
}
}
}
}
|