Java Date GMT Format getStringBaseGMT(Date date, String timezone)

Here you can find the source of getStringBaseGMT(Date date, String timezone)

Description

get String Base GMT

License

Apache License

Declaration

public static String getStringBaseGMT(Date date, String timezone) 

Method Source Code

//package com.java2s;
/*/*from   ww  w.  j a  v  a 2 s  .  com*/
 * Commons-Utils
 * Copyright (c) 2017.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 */

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String getStringBaseGMT(Date date, String timezone) {
        return dateToString(getDateBaseGMT(date, timezone));
    }

    public static String dateToString(Date date, String pattern) {
        return dateToString(date, pattern, TimeZone.getDefault());
    }

    public static String dateToString(Date date) {
        return dateToString(date, YYYY_MM_DD_HH_MM_SS, TimeZone.getDefault());
    }

    public static String dateToString(Date date, String pattern, TimeZone timezone) {
        SimpleDateFormat dateFormat = null;
        if (null == pattern || "".equals(pattern)) {
            dateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        } else {
            dateFormat = new SimpleDateFormat(pattern);
        }
        dateFormat.setTimeZone(timezone);
        return dateFormat.format(date);
    }

    public static Date getDateBaseGMT(Date date, String timezone) {
        TimeZone srcZone = TimeZone.getTimeZone("GMT");
        TimeZone toZone = TimeZone.getTimeZone(timezone);
        Long targetTime = date.getTime() - srcZone.getRawOffset() + toZone.getRawOffset();
        return new Date(targetTime);
    }
}

Related

  1. formatGmtDate(Date date)
  2. formatGMTDate(Date gmtTime, int offset)
  3. getDefaultID2GMT()
  4. getFormattedGMTDate(long timestamp)
  5. getNewGmtSimpleDateFormat(String format)
  6. getThreadLocalGMTDateFormat(final String format)
  7. getTimeAndDateAtGMT()
  8. getTzToGmt(String dateStr, String dateFormat, String beforeTimeZone, String afterTimeZone)
  9. parseDate(String gmtTimeString)