Java Timestamp Format formatGMTTimestampToDate(Timestamp time)

Here you can find the source of formatGMTTimestampToDate(Timestamp time)

Description

format GMT Timestamp To Date

License

Open Source License

Declaration

public static String formatGMTTimestampToDate(Timestamp time) 

Method Source Code

//package com.java2s;

import java.sql.Timestamp;
import java.text.*;

import java.util.TimeZone;

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

    public static String formatGMTTimestampToDate(Timestamp time) {
        return toStringFormatGMTTime(time, DEFAULT_DATE_YMD_FORMAT);
    }//from   w  ww .j a  va  2s  .c  o m

    public static String formatGMTTimestampToDate(Timestamp time, String dateStyleId) {
        return toStringFormatGMTTime(time, getYMDFormat(dateStyleId));
    }

    public static String formatGMTTimestampToDate(long time) {
        return toStringFormatGMTTime(time, DEFAULT_DATE_YMD_FORMAT);
    }

    public static String formatGMTTimestampToDate(long time, String dateStyleId) {
        return toStringFormatGMTTime(time, getYMDFormat(dateStyleId));
    }

    public static String toStringFormatGMTTime(Timestamp time, String format) {

        if (time == null) {
            return null;
        }

        return toStringFormatGMTTime(time.getTime(), format);
    }

    public static String toStringFormatGMTTime(long time, String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

        return sdf.format(new java.util.Date(time));
    }

    public static String getYMDFormat(String datePtn) {
        final String[][] DATE_FORMAT_YMD_LIST = { { "1", "yyyy/MM/dd" }, { "2", "yyyy.MM.dd" },
                { "3", "yyyy-MM-dd" }, { "4", "MM/dd/yyyy" }, { "5", "MM.dd.yyyy" }, { "6", "MM-dd-yyyy" },
                { "7", "dd/MM/yyyy" }, { "8", "dd.MM.yyyy" }, { "9", "dd-MM-yyyy" }, { "A", "dd/MM yyyy" } };
        String format = null;

        for (int i = 0; i < DATE_FORMAT_YMD_LIST.length; i++) {
            if (DATE_FORMAT_YMD_LIST[i][0].equals(datePtn)) {
                format = DATE_FORMAT_YMD_LIST[i][1];
                break;
            }
        }

        if (format == null) {
            throw new IllegalArgumentException("The value of an argument is inaccurate.");
        }
        return format;
    }
}

Related

  1. formatDateWithCinderellaTime(Date date)
  2. formatDefaultDate(Timestamp time)
  3. formatDuration(final long duration)
  4. formatElapsed(long elapsed)
  5. formatFileSize(Number data)
  6. formatJDBCTimeStamp(final java.util.Date date)
  7. formatJDBCTimeStamp(final java.util.Date date)
  8. formatMillis(long _millis)
  9. formatShortDateTime(Timestamp time)