Java Timestamp from convertToTimestamp(String dateData)

Here you can find the source of convertToTimestamp(String dateData)

Description

convert To Timestamp

License

Open Source License

Declaration

public static Timestamp convertToTimestamp(String dateData) throws Exception 

Method Source Code


//package com.java2s;
import java.sql.Timestamp;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {

    public static Timestamp convertToTimestamp(String dateData) throws Exception {

        try {//from  w  w w  .  j  a  v a2 s .co m

            if (dateData == null)
                return null;
            if (dateData.trim().equals(""))
                return null;

            int dateObjLength = dateData.length();

            String yearString = "2002";
            String monthString = "01";
            String dayString = "01";

            if (dateObjLength >= 4) {
                yearString = dateData.substring(0, 4);
            }
            if (dateObjLength >= 6) {
                monthString = dateData.substring(4, 6);
            }
            if (dateObjLength >= 8) {
                dayString = dateData.substring(6, 8);
            }

            int year = Integer.parseInt(yearString);
            int month = Integer.parseInt(monthString) - 1;
            int day = Integer.parseInt(dayString);

            Calendar cal = new GregorianCalendar();
            cal.set(year, month, day);
            //cal.getTime();
            return new Timestamp(cal.getTime().getTime());

        } catch (Exception e) {
            throw new Exception("[DateUtil][convertToTimestamp]" + e.getMessage(), e);
        }

    }
}

Related

  1. convertStringDateToTimestamp(String sDate, String dateformat)
  2. convertStringToTimeStamp(String date)
  3. convertStringToTimestamp(String str)
  4. convertToTimestamp(Date aJavaDate)
  5. convertToTimestamp(java.util.Date date)
  6. convertToTimestamp(String dateString, String pattern)
  7. convertToTimestamp(String s)
  8. convertToTimeStamp(Timestamp timestamp, String format, TimeZone timeZone)
  9. convertToTimestampDate(Date date)