Java Time in GMT getGMTByDisplay(String tm)

Here you can find the source of getGMTByDisplay(String tm)

Description

get special time zone offset

License

Open Source License

Parameter

Parameter Description
tm String

Return

String

Declaration

public static String getGMTByDisplay(String tm) 

Method Source Code


//package com.java2s;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;

public class Main {
    private static Map<String, StringBuffer> normtoIDsMap;

    /**//from w w w .j  a va 2  s .c  o m
     * get special time zone offset
     * 
     * @param tm String
     * @return String
     */
    public static String getGMTByDisplay(String tm) {
        String gmt = null;
        if (null == tm || "Default".equals(tm)) {
            return getDefaultID2GMT();
        }
        for (Entry<String, StringBuffer> en : normtoIDsMap.entrySet()) {
            String enKey = en.getValue().toString();
            if (enKey.indexOf(tm) >= 0 || enKey.equalsIgnoreCase(tm)) {
                gmt = en.getKey();
                break;
            }
        }

        return gmt;
    }

    /**
     * default timezone to GMT
     * 
     * @return string
     */
    public static String getDefaultID2GMT() {
        return getGMTFormat(TimeZone.getDefault().getID());
    }

    /**
     * get spec timezone offset
     * 
     * @param tm String
     * @return String
     */
    public static String getGMTFormat(String tm) {
        TimeZone timeZone;
        if (tm == null) {
            timeZone = TimeZone.getDefault();
        } else {
            timeZone = TimeZone.getTimeZone(tm);
        }
        return getGMTFormat(timeZone);
    }

    /**
     * return timezone for a given offset
     * 
     * @param rawOffset int
     * @return String
     */
    public static String getGMTFormat(int rawOffset) {
        int hour = rawOffset / (1000 * 60 * 60);
        NumberFormat format = new DecimalFormat("00':00'");

        return hour >= 0 ? "GMT+" + format.format(hour) : "GMT" + format.format(hour);
    }

    /**
     * get spec timezone offset
     * 
     * @param tz TimeZone
     * @return String
     */
    private static String getGMTFormat(TimeZone tz) {
        return getGMTFormat(tz.getRawOffset());
    }

    /**
     * Transform the time in milliseconds to the time in "dd hh:mm:ss.SSS"
     * format.
     * 
     * @param ms milliseconds
     * @return dd hh:mm:ss.SSS
     */
    public static String format(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        String strDay = day < 10 ? "0" + day : "" + day;
        String strHour = hour < 10 ? "0" + hour : "" + hour;
        String strMinute = minute < 10 ? "0" + minute : "" + minute;
        String strSecond = second < 10 ? "0" + second : "" + second;
        String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
        return new StringBuffer(strDay).append(" ").append(strHour).append(":").append(strMinute).append(":")
                .append(strSecond).append(".").append(strMilliSecond).toString();
    }
}

Related

  1. getGMT(Date date)
  2. getGMT8Time()
  3. getGMTCalendarAtTime(String sTime)
  4. getGMTDate()
  5. getGMTDateFormat(String format)
  6. getGMTDateFormat(String strFormat)