Convert String to Timestamp Java

12.8k views Asked by At

So I am having this issue that I can't wrap my head around. I've read similar questions posed but very case I've found there is an issue with the format, and my format is correct.

Basically I am trying to convert a String into a Timestamp, and I get the unparseable date error.

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Hello {

public static Timestamp convertStringToTimestamp(String str_date) {
    try {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
        Date date = formatter.parse(str_date);
        java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
        return timeStampDate;

    } catch (ParseException e) {
        System.out.println("Exception :" + e);
        return null;
    }
}

public static void main(String[] args) {
    Timestamp ts = convertStringToTimestamp("2015-06-09 11:51:12,708");
    Timestamp ts2 = convertStringToTimestamp("2015-04-17 11:29:49.564");
    System.out.println(ts +" | "+ts2);


}

}

Output:

Exception :java.text.ParseException: Unparseable date: "2015-06-09 11:51:12,708"
Exception :java.text.ParseException: Unparseable date: "2015-04-17 11:29:49.564"
null | null

Any ideas?

4

There are 4 answers

6
ASP On BEST ANSWER
**Update**
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class SomeClass {

    public static void main(String[] args) {


         System.out.println(convertStringToTimestamp("2015-06-09 11:51:12,708"));
         //be consistent here with , and .
         System.out.println(convertStringToTimestamp("2015-04-17 11:29:49.564"));
         System.out.println();


    }

    private static Timestamp  convertStringToTimestamp(String something) {


SimpleDateFormat dateFormat = null;
        if(something.contains(".")) {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        } 
        if(something.contains(",")) {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
        }
        Timestamp timestamp = null;
            Date parsedDate;
            try {
                parsedDate = dateFormat.parse(something);
                 timestamp = new java.sql.Timestamp(parsedDate.getTime());

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return timestamp;
    }



}
3
Eduardo Briguenti Vieira On

This works perfectly to me.

I just passed the right pattern as an input as well.

public static Timestamp convertStringToTimestamp(String str_date, String pattern) {
        try {

            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            Date date = formatter.parse(str_date);
            java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
            return timeStampDate;

        } catch (ParseException e) {
            System.out.println("Exception :" + e);
            return null;
        }
    }

    public static void main(String[] args) {
        Timestamp ts = convertStringToTimestamp("2015-06-09 11:51:12,708", "yyyy-MM-dd HH:mm:ss,SSS");
        Timestamp ts2 = convertStringToTimestamp("2015-04-17 11:29:49.564", "yyyy-MM-dd HH:mm:ss.SSS");
        System.out.println(ts +" | "+ts2);


    }

The output is:

2015-06-09 11:51:12.708 | 2015-04-17 11:29:49.564
0
Sezin Karli On

"2015-06-09 11:51:12,708" is working for me but "2015-04-17 11:29:49.564" won't. You specified the regex for "," so "." would not. It is perfectly normal.

0
Mudassar On

you need to fix the comma

Timestamp ts2 = convertStringToTimestamp("2015-04-17 11:29:49.564");