Java SQL Time toGMTTime(long local)

Here you can find the source of toGMTTime(long local)

Description

to GMT Time

License

Open Source License

Declaration

public static long toGMTTime(long local) 

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 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;
        }/* ww  w .j  a va  2 s  . c  o  m*/

        return date.getTime();
    }

    public static long toGMTTime(Timestamp local) {

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

        return toGMTTime(local.getTime());
    }

    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. StrToDateTime(String val)
  2. strToTime(String strDate)
  3. toDate(TimeZone tz, int days)
  4. toDateFromTime(String time)
  5. todayBeginTime()
  6. toMySQLDate(LocalDateTime dateTime)
  7. toPOSIXTime(String timeString)
  8. truncateTimePartOfDate(Date date)
  9. unformattedFromTime(java.sql.Time t)