Java SQL Date Create getSysDateGMTToday()

Here you can find the source of getSysDateGMTToday()

Description

get Sys Date GMT Today

License

Open Source License

Declaration

public static Date getSysDateGMTToday() 

Method Source Code


//package com.java2s;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.*;
import java.util.Calendar;

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 Date getSysDateGMTToday() {

        long time = getSystemTimeGMTToday();

        if (time == -1) {
            return null;
        }/*from w w  w . j  a  va 2 s  . c  o  m*/

        return new Date(getSystemTimeGMTToday());
    }

    public static long getSystemTimeGMTToday() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

        Timestamp today = toTimestamp(sdf.format(Calendar.getInstance().getTime()) + " 00:00:00");

        return toGMTTime(today);
    }

    public static Timestamp toTimestamp(String str) {

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

        try {
            return Timestamp.valueOf(str.trim());
        } catch (IllegalArgumentException iae) {
            return null;
        }

    }

    public static Timestamp toTimestamp(String str, String format) {

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

        try {
            return new Timestamp(parseDate(str, format).getTime());
        } catch (Exception e) {
            return null;
        }

    }

    public static long toGMTTime(long local) {

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

        ParsePosition pos = new ParsePosition(0);
        java.util.Date date = sdf.parse(new Timestamp(local).toString(), pos);

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

        return date.getTime();
    }

    public static long toGMTTime(Timestamp local) {

        if (local == null) {
            return -1;
        }

        return toGMTTime(local.getTime());
    }

    public static java.util.Date parseDate(String value, String pattern) {
        try {
            TimeZone tz = TimeZone.getDefault();
            String dateFormat = pattern;
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            sdf.setTimeZone(tz);

            // Parse date
            java.util.Date parsed = null;

            parsed = sdf.parse(value);
            return parsed;
        } catch (ParseException e) {
            return null;
        }
    }

    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. getSqlDate()
  2. getSQLDate()
  3. getSysDate()
  4. getSysDate()
  5. getSysdate(String format)
  6. getTodayDate()
  7. getTodayDate()
  8. getTodaySqlDate()
  9. getTodaySqlDate()