Java Timestamp Create toTimestamp(Object dateobj)

Here you can find the source of toTimestamp(Object dateobj)

Description

to Timestamp

License

LGPL

Declaration

public static java.sql.Timestamp toTimestamp(Object dateobj) throws Exception 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.Date;
import java.text.*;

public class Main {
    public static java.sql.Timestamp toTimestamp(Object dateobj) throws Exception {
        return toTimestamp(dateobj.toString());
    }/*from  ww  w  .j  ava2 s  .  c  o  m*/

    /**
     * Converts date from String to sql timeStamp type using the default Format (EEE MMM dd H:mm:ss z yyyy).
     * @param DateString
     * @return java.sql.Timestamp
     * @throws Exception
     */
    public static java.sql.Timestamp toTimestamp(String DateString) throws Exception {
        long longDate = (long) new Date().getTime();
        try {
            java.sql.Date sdt;
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd H:mm:ss z yyyy");
            java.util.Date udt = (java.util.Date) sdf.parse(DateString);

            longDate = (long) udt.UTC(udt.getYear(), udt.getMonth(), udt.getDate(), udt.getHours(),
                    udt.getMinutes(), udt.getSeconds());
        } catch (ParseException e) {
            throw new Exception(e.getMessage());
        }
        return new java.sql.Timestamp(longDate);
    }
}

Related

  1. toTimestamp(final java.sql.Date date)
  2. toTimestamp(final String pValue)
  3. toTimestamp(java.sql.Date date)
  4. toTimestamp(long ts)
  5. toTimestamp(long value)
  6. toTimestamp(Object objInParam)
  7. toTimestamp(String _sDate)
  8. toTimestamp(String dateTime)
  9. toTimestamp(String dateTime)