Java Timestamp checkTimestamp(String sval)

Here you can find the source of checkTimestamp(String sval)

Description

Timestamp type validation and conversion.

License

Open Source License

Parameter

Parameter Description
sval String representation of desired Timestamp

Return

Calendar instance reflecting string interpreted as Timestamp in ALU_DF format

Declaration

public static Timestamp checkTimestamp(String sval) 

Method Source Code

//package com.java2s;
/*//from   www  .  j  ava  2 s . c  om
 * This file is part of wnmsextract.
 * 
 * wnmsextract is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * wnmsextract 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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

import java.util.Calendar;

public class Main {
    public static final DateFormat ALUDB_DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * Timestamp type validation and conversion. High overhead but necessary on untrustworty sources
     * @param sval String representation of desired Timestamp
     * @return Calendar instance reflecting string interpreted as Timestamp in {@link com.alcatel_lucent.nz.wnmsextract.database.ALUDBUtilities} ALU_DF format 
     */
    public static Timestamp checkTimestamp(String sval) {
        Calendar cal = Calendar.getInstance();
        cal.set(2000, 0, 1);
        Timestamp tval = new Timestamp(cal.getTime().getTime());
        try {
            tval = new Timestamp(ALUDB_DF.parse(sval).getTime());
        } catch (ParseException pe) {
            System.err.println("Timestamp parse exception, def to '2000-01-01' :: " + pe);
        } catch (NumberFormatException nfe) {
            System.err.println("Timestamp format exception, def to '2000-01-01' :: " + nfe);
        }
        return tval;

    }
}

Related

  1. calcRealTime(Timestamp beginTime, Timestamp endTime)
  2. calendar2Timestamp(Calendar c)
  3. calendarToTimestamp(java.util.Calendar inCal)
  4. calToTimeStamp(Calendar cal)
  5. changeDate(Timestamp date, Integer amount, Integer unit)
  6. clone(Timestamp original)
  7. concatDateAndTime(Timestamp date, Time time)
  8. convertTS2HHMM(Timestamp timeStamp)
  9. copyOrCreateTimestampNullsafe(Date date)