Java Timestamp Create getTimestamp(String dateString, String format)

Here you can find the source of getTimestamp(String dateString, String format)

Description

get Timestamp

License

LGPL

Declaration

public static Timestamp getTimestamp(String dateString, String format)
            throws Exception 

Method Source Code

//package com.java2s;
/*//from   w w w . jav  a2s .c  o m
 *   This software is distributed under the terms of the FSF 
 *   Gnu Lesser General Public License (see lgpl.txt). 
 *
 *   This program is distributed WITHOUT ANY WARRANTY. See the
 *   GNU General Public License for more details.
 */

import java.sql.Timestamp;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    public static Timestamp getTimestamp(String dateString, String format)
            throws Exception {
        Date date = getDate(dateString, format);
        if (date == null)
            return null;

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

    public static Date getDate(String dateString, String format, TimeZone tz)
            throws Exception {
        if (dateString == null || format == null || dateString.equals("")
                || tz == null)
            return null;

        SimpleDateFormat formatter = new SimpleDateFormat(format);
        formatter.setTimeZone(tz);
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(dateString, pos);
    }

    public static Date getDate(String dateString, String format)
            throws Exception {
        return getDate(dateString, format, TimeZone.getDefault());
    }
}

Related

  1. getTimestamp(Object value, int columnType)
  2. getTimestamp(ResultSet rs, String colName)
  3. getTimestamp(ResultSet rs, String strColName)
  4. getTimestamp(String date, String time)
  5. getTimestamp(String dateStr)
  6. getTimestamp(String sqlDateTime)
  7. getTimestamp(String Str)
  8. getTimestamp(String str)
  9. getTimestamp(String strDate)