Java Date GMT Format getDefaultID2GMT()

Here you can find the source of getDefaultID2GMT()

Description

default timezone to GMT

License

Open Source License

Return

string

Declaration

public static String getDefaultID2GMT() 

Method Source Code


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

import java.util.TimeZone;

public class Main {
    /**/*from  www.  ja  va  2 s. c om*/
     * 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. formatDateTimeGMT(Date date)
  2. formatGMT(Date date)
  3. formatGMT(Date date, String parttern)
  4. formatGmtDate(Date date)
  5. formatGMTDate(Date gmtTime, int offset)
  6. getFormattedGMTDate(long timestamp)
  7. getNewGmtSimpleDateFormat(String format)
  8. getStringBaseGMT(Date date, String timezone)
  9. getThreadLocalGMTDateFormat(final String format)