Java Date GMT Format toStringWithoutGMT(Date date)

Here you can find the source of toStringWithoutGMT(Date date)

Description

to String Without GMT

License

Open Source License

Declaration

public static String toStringWithoutGMT(Date date) 

Method Source Code

//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    public static final String _DATE_MAX = "9999-12-31 23:59:59";

    public static String toStringWithoutGMT(Date date) {
        return toStringWithoutGMT(date, null);
    }//  ww w .  j  av a2 s  . c  o  m

    public static String toStringWithoutGMT(Date date, TimeZone tz) {
        if (tz == null)
            tz = TimeZone.getDefault();
        if (date.after(getMax()))
            date = getMax();
        SimpleDateFormat outputXmlFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        outputXmlFmt.setTimeZone(tz);
        return outputXmlFmt.format(date);
    }

    public static Date getMax() {
        return toDate(_DATE_MAX);
    }

    /**
     * This function is use to convert string date format to data format
     * @param dateStr
     * @return 
     */
    public static Date toDate(String dateStr) {
        return toDate(dateStr, null);
    }

    /**
     * Convert string date format with time zone
     * @param dateStr
     * @param tz
     * @return 
     */
    public static Date toDate(String dateStr, TimeZone tz) {
        if (tz == null)
            tz = TimeZone.getDefault();
        if (dateStr == null)
            return null;

        SimpleDateFormat fmt = null;
        if (dateStr.length() == 19) {
            fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            fmt.setTimeZone(tz);
        } else {
            fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
        }
        try {
            return fmt.parse(dateStr);
        } catch (ParseException e) {

            return null;
        }
    }
}

Related

  1. parseGMTDatetime(String str)
  2. parseGMTTime(Long time, String format)
  3. timeStampGMT(Date date)
  4. toGMTString(Date d)
  5. toGMTString(Date dtIn)