Java Timestamp from convertDateStringToTimestamp(String dateString, String format)

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

Description

convert Date String To Timestamp

License

LGPL

Declaration

public static Timestamp convertDateStringToTimestamp(String dateString, String format) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.sql.Timestamp;

import java.util.StringTokenizer;

public class Main {
    public static Timestamp convertDateStringToTimestamp(String dateString, String format) {
        String day = "";
        String month = "";
        String year = "";
        String hour = "";
        String minute = "";
        String second = "";
        StringTokenizer st;//from  w w  w .  java 2  s.  c  o m
        Timestamp dateTime = null;
        try {
            if (format.equalsIgnoreCase("dd/mm/yy")) {
                day = dateString.substring(0, 2);
                month = dateString.substring(3, 5);
                year = "20" + dateString.substring(6, 8);
                hour = "00";
                minute = "00";
                second = "00";
            } else if (format.equalsIgnoreCase("dd/mm/yyyy")) {
                st = new StringTokenizer(dateString, "/");
                day = st.nextToken(); //dateString.substring(0, 2);
                month = st.nextToken(); //dateString.substring(3, 5);
                year = st.nextToken(); //dateString.substring(6, 10);
                if (day.length() == 1)
                    day = "0" + day;
                if (month.length() == 1)
                    month = "0" + month;
                hour = "00";
                minute = "00";
                second = "00";
            } else if (format.equalsIgnoreCase("yyyy-mm-dd")) {
                day = dateString.substring(8, 10);
                month = dateString.substring(5, 7);
                year = dateString.substring(0, 4);
                hour = "00";
                minute = "00";
                second = "00";
            } else if (format.equalsIgnoreCase("yyyy-mm-dd hh:mm:ss")) {
                day = dateString.substring(8, 10);
                month = dateString.substring(5, 7);
                year = dateString.substring(0, 4);
                hour = dateString.substring(11, 13);
                minute = dateString.substring(14, 16);
                second = dateString.substring(17, 19);
            }
            String currTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
                    + ".000000000";
            dateTime = Timestamp.valueOf(currTime);
        } catch (Exception e) {
            e.printStackTrace();
            dateTime = null;
        }
        return dateTime;
    }
}

Related

  1. convertDateToTimestamp(Date date)
  2. convertDateToTimestamp(java.util.Date date)
  3. convertLongToTimestamp(Long millSec)
  4. convertSQLTimestamp(String s, String formatIn, String formatOut)