Java Timestamp from convertSQLTimestamp(String s, String formatIn, String formatOut)

Here you can find the source of convertSQLTimestamp(String s, String formatIn, String formatOut)

Description

returns a SQL Timestamp given a formatted string

License

Open Source License

Declaration

public static java.sql.Timestamp convertSQLTimestamp(String s, String formatIn, String formatOut) 

Method Source Code


//package com.java2s;
/* $This file is distributed under the terms of the license in /doc/license.txt$ */

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

public class Main {
    /**//from ww  w .  j av a  2 s  .  co m
     * returns a SQL Timestamp given a formatted string
     */
    public static java.sql.Timestamp convertSQLTimestamp(String s, String formatIn, String formatOut) {
        String s2 = convertStringDate(s, formatIn, formatOut);
        //System.out.println("s2: "+s2);
        return java.sql.Timestamp.valueOf(s2 + ".0");
    }

    /**
     * given a String date in a described format (i.e. YYYY-mm-dd) convert
     * it to a String date in a new format (i.e. MM-dd-YYYY)
     *
     * @param s input date string
     * @param formatin a format string
     * @param formatout a format string
     * @return formated date string
     */
    public static String convertStringDate(String s, String formatIn, String formatOut) {

        try {
            if (s.equals("now")) {
                return (new SimpleDateFormat(formatOut).format(new java.util.Date()));
            } else {
                return (new SimpleDateFormat(formatOut))
                        .format((new SimpleDateFormat(formatIn)).parse(s, new ParsePosition(0)));
            }
        } catch (Exception e) {
            return "";
        }

    }
}

Related

  1. convertDateStringToTimestamp(String dateString, String format)
  2. convertDateToTimestamp(Date date)
  3. convertDateToTimestamp(java.util.Date date)
  4. convertLongToTimestamp(Long millSec)
  5. convertString2TimeStamp(String dateString)
  6. convertString2Timestamp(String value)
  7. convertStringDateToTimestamp(String sDate, String dateformat)
  8. convertStringToTimeStamp(String date)