Java Timestamp Parse stringToTimestamp(String aS_Timestamp, String aS_Format, Timestamp aTs_ValidStart, Timestamp aTs_ValidEnd)

Here you can find the source of stringToTimestamp(String aS_Timestamp, String aS_Format, Timestamp aTs_ValidStart, Timestamp aTs_ValidEnd)

Description

Converts string to java.sql.Timestamp using format string

License

Open Source License

Parameter

Parameter Description
aS_Timestamp String containing timestamp
aS_Format String containing timestamp format

Return

java.sql.Timestamp object if ok, else null

Declaration

public static java.sql.Timestamp stringToTimestamp(String aS_Timestamp, String aS_Format,
        Timestamp aTs_ValidStart, Timestamp aTs_ValidEnd) 

Method Source Code

//package com.java2s;
/*//from   w  ww  .ja  v a  2 s  .c  o m
NetForm Library
---------------
Copyright (C) 2001-2005 - Sampsa Sohlman, Teemu Sohlman
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
*/

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {
    /**
     * Converts string to java.sql.Timestamp using format string
     * @param aS_Timestamp String containing timestamp
     * @param aS_Format String containing timestamp format
     * @return java.sql.Timestamp object if ok, else null
     * @see java.text.SimpleDateFormat for format options
     */
    public static java.sql.Timestamp stringToTimestamp(String aS_Timestamp, String aS_Format,
            Timestamp aTs_ValidStart, Timestamp aTs_ValidEnd) {
        SimpleDateFormat l_SimpleDateFormat;
        java.sql.Timestamp l_Timestamp;
        java.util.Date l_Date = null;

        if (aS_Timestamp == null || aS_Format == null) {
            return null;
        }

        l_SimpleDateFormat = new SimpleDateFormat(aS_Format);
        try {
            l_Date = l_SimpleDateFormat.parse(aS_Timestamp);
            l_Timestamp = new java.sql.Timestamp(l_Date.getTime());

            if (aTs_ValidStart == null || aTs_ValidEnd == null) {
                return l_Timestamp;
            }

            if (aTs_ValidStart.before(l_Timestamp) && aTs_ValidEnd.after(l_Timestamp)) {
                return l_Timestamp;
            } else {
                return null;
            }
        } catch (ParseException a_ParseException) {
            return null;
        }
    }

    public static java.sql.Timestamp stringToTimestamp(String aS_Timestamp, String aS_Format) {
        return stringToTimestamp(aS_Timestamp, aS_Format, null, null);

    }
}

Related

  1. string2Timestamp(String strDateTime, String pattern)
  2. String2Timestamp(String strInputDate)
  3. string2Timestamp(String value)
  4. stringToTimestamp()
  5. stringToTimestamp(final String dateString)
  6. stringToTimestamp(String date, String formatStr)
  7. stringToTimeStamp(String dateTimeString, String dateTimeFormat, TimeZone tz, Locale locale)
  8. stringToTimestamp(String marcTimestamp)
  9. stringToTimestamp(String sTimestamp)