Java Timestamp Create toTimestamp(String str, String format)

Here you can find the source of toTimestamp(String str, String format)

Description

to Timestamp

License

Open Source License

Declaration

public static Timestamp toTimestamp(String str, String format) 

Method Source Code

//package com.java2s;

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

import java.util.TimeZone;

public class Main {
    public static Timestamp toTimestamp(String str) {

        if (str == null) {
            return null;
        }/*from  w  w w . ja  v a  2s  .co  m*/

        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 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;
        }
    }
}

Related

  1. toTimestamp(String dateTime)
  2. toTimestamp(String dateTime)
  3. toTimestamp(String dt)
  4. toTimestamp(String s)
  5. toTimeStamp(String sDate, String format)
  6. toTimestamp(String value)
  7. toTimestamp(String yyyymmddhhmmss)
  8. toTimestamp2(long seconds, int fraction, int width)
  9. toTimestamp2(long seconds, int nanos)