summaryrefslogtreecommitdiffstats
path: root/src/org/jogamp/jabot/util/TimeTool.java
blob: de9036178d09de0a50fa8eb0f86de3e6aa70e076 (plain)
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
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 (TMZ) */
    public static String getTimeStamp(final Calendar calendar) {
        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);
        }
        return String.format("%04d%02d%02d %02d:%02d:%02d (%s)", 
                    year, month, day, hour, minute, seconds, calendar.getTimeZone().getID());
    }

}