Java Date GMT Format formatGMTDate(Date gmtTime, int offset)

Here you can find the source of formatGMTDate(Date gmtTime, int offset)

Description

format date to show in web pages, with default time format, since 2005/09/13

License

Apache License

Parameter

Parameter Description
gmtTime GMT time, get from DB
offset timezone offset, can get from user information

Return

string data to show in web pages, in user timezone

Declaration

public static String formatGMTDate(Date gmtTime, 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";

    /**/*  w w  w .ja va2  s  .co  m*/
     * format date to show in web pages, since 2005/09/13
     * 
     * @param gmtTime
     *            GMT time, get from DB
     * @param format
     *            time format
     * @param offset
     *            timezone offset, can get from user information
     * @return string data to show in web pages, in user timezone
     */
    public static String formatGMTDate(Date gmtTime, String format, int offset) {
        Date localTime = getLocalFromGMT(gmtTime, offset);
        return formatDate(localTime, format);
    }

    /**
     * format date to show in web pages, with default time format, since
     * 2005/09/13
     * 
     * @param gmtTime
     *            GMT time, get from DB
     * @param offset
     *            timezone offset, can get from user information
     * @return string data to show in web pages, in user timezone
     */
    public static String formatGMTDate(Date gmtTime, int offset) {
        return formatGMTDate(gmtTime, DEFAULT_DATE_FORMAT, offset);
    }

    /**
     * Convert a GMT time to a local time, the local time zone is specified by
     * offset
     * 
     * @param gmtTime
     * @param offset
     *            - offset to GMT time in term of hours, e.g. +1, +2, etc.
     * 
     * @return Local time
     */
    public static Date getLocalFromGMT(Date gmtTime, 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(gmtTime, TimeZone.getTimeZone("GMT"), tz1);
    }

    /**
     * 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);
    }

    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. formatDateTimeGMT(Date date)
  2. formatGMT(Date date)
  3. formatGMT(Date date, String parttern)
  4. formatGmtDate(Date date)
  5. getDefaultID2GMT()
  6. getFormattedGMTDate(long timestamp)
  7. getNewGmtSimpleDateFormat(String format)
  8. getStringBaseGMT(Date date, String timezone)