Java Time in GMT getGMTTimeStr(Date pageTime, int offset)

Here you can find the source of getGMTTimeStr(Date pageTime, int offset)

Description

get GMT Time string for given page date and offset, time would be formated to DEFAULT_DATE_FORMAT;

License

Apache License

Parameter

Parameter Description
date a parameter
offset a parameter

Declaration

public static String getGMTTimeStr(Date pageTime, int offset) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import java.util.TimeZone;

public class Main {
    public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    /**/*from   www . j a va2  s  .com*/
     * get GMT Time string for given page date and offset, time would be
     * formated to DEFAULT_DATE_FORMAT;
     * 
     * @param date
     * @param offset
     * @return
     */
    public static String getGMTTimeStr(Date pageTime, int offset) {
        return formatDate(getGMTFromLocal(pageTime, offset), DEFAULT_DATE_FORMAT);
    }

    /**
     * Format date by the format
     * 
     * @param date
     *            - the source date
     * @param format
     *            - the date format
     * 
     * @return the result String
     */
    public static String formatDate(Date date, String format) {
        if (date == null)
            return null;

        Format formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * Convert a local time to GMT time, the local time zone is specified by
     * offset
     * 
     * @param localTime
     * @param offset
     *            - offset to GMT time in term of hours, e.g. +1, +2, etc.
     * 
     * @return GMT time
     */
    public static Date getGMTFromLocal(Date localTime, int offset) {
        TimeZone tz1 = TimeZone.getTimeZone("GMT");
        if (offset > 0)
            tz1 = TimeZone.getTimeZone("GMT+" + String.valueOf(offset));
        else if (offset < 0)
            tz1 = TimeZone.getTimeZone("GMT" + String.valueOf(offset));

        return getTime(localTime, tz1, TimeZone.getTimeZone("GMT"));

    }

    public static Date getTime() {
        return Calendar.getInstance().getTime();
    }

    public static Date getTime(int field, int diff) {
        Calendar c = Calendar.getInstance();
        c.add(field, diff);
        return c.getTime();
    }

    /**
     * get time of dstTimeZone from time of srcTimeZone
     * 
     * @param time
     * @param srcTimeZone
     * @param dstTimeZone
     * @return Data
     */
    private static Date getTime(Date time, TimeZone srcTimeZone, TimeZone dstTimeZone) {
        if (time == null)
            return null;
        DateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        df.setTimeZone(dstTimeZone);
        String gmtTime = df.format(time); // convert current time to gmt time.
        df.setTimeZone(srcTimeZone);
        try {
            return df.parse(gmtTime); // gmt time to date
        } catch (ParseException e) {
            return time;
        }

    }

    public static Date parse(String date, String format) throws ParseException {
        if (date == null)
            return null;

        SimpleDateFormat fmt = new SimpleDateFormat(format);

        return fmt.parse(date);
    }
}

Related

  1. getGMTime()
  2. getGMTString(Date d)
  3. getGMTString(Date date, TimeZone tz)
  4. getGMTTime(Date localTime)
  5. getGMTTime(final Date date)
  6. getGmtTimeString(double startTime)