Java Timestamp Create toTimestampFromGMT(int yy, int mm, int dd, int hh, int mi, int ss)

Here you can find the source of toTimestampFromGMT(int yy, int mm, int dd, int hh, int mi, int ss)

Description

to Timestamp From GMT

License

Open Source License

Declaration

public static Timestamp toTimestampFromGMT(int yy, int mm, int dd, int hh, int mi, int ss) 

Method Source Code


//package com.java2s;
import java.sql.Date;
import java.sql.Time;
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 final String DEFAULT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    public static Timestamp toTimestampFromGMT(int yy, int mm, int dd, int hh, int mi, int ss) {
        return toTimestampFromGMT(String.valueOf(yy), String.valueOf(mm), String.valueOf(dd), String.valueOf(hh),
                String.valueOf(mi), String.valueOf(ss));
    }/*from w w  w.ja  va  2s  .co  m*/

    public static Timestamp toTimestampFromGMT(String yy, String mm, String dd, String hh, String mi, String ss) {

        mm = mm != null && mm.length() == 1 ? "0" + mm : mm;
        dd = dd != null && dd.length() == 1 ? "0" + dd : dd;
        hh = hh != null && hh.length() == 1 ? "0" + hh : hh;
        mi = mi != null && mi.length() == 1 ? "0" + mi : mi;
        ss = ss != null && ss.length() == 1 ? "0" + ss : ss;

        return toTimestampFromGMT(yy + "-" + mm + "-" + dd + " " + hh + ":" + mi + ":" + ss);
    }

    public static Timestamp toTimestampFromGMT(String str) {

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

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

        ParsePosition pos = new ParsePosition(0);
        java.util.Date date = sdf.parse(str, pos);
        if (date == null) {
            return null;
        }

        return new Timestamp(date.getTime());
    }

    public static Timestamp toTimestampFromGMT(Timestamp time) {
        return toTimestampFromGMT(time.toString());
    }

    public static String toString(Date date) {
        return toString((java.util.Date) date);
    }

    public static String toString(java.util.Date date) {
        return toString(date, DEFAULT_DATE_YMD_FORMAT);
    }

    public static String toString(Date date, String format) {
        return toString((java.util.Date) date, format);
    }

    public static String toString(java.util.Date date, String format) {

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

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

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(date);
    }

    public static String toString(Time time, String format) {

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

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

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(time);
    }

    public static String toString(Time time) {
        return toString(time, DEFAULT_TIME_FORMAT);
    }
}

Related

  1. toTimestamp(String yyyymmddhhmmss)
  2. toTimestamp2(long seconds, int fraction, int width)
  3. toTimestamp2(long seconds, int nanos)
  4. toTimestamp2(long value, int nanos, int meta)
  5. toTimestampByHour(Date date, int hour)
  6. toTimestampFromLong(long millis)
  7. toTimestampFromTime(String time)