0

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?

commit
  • 4,777
  • 15
  • 43
  • 70
jensd
  • 51
  • 1
  • 2
  • 6

4 Answers4

0

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
Eduardo Briguenti Vieira
  • 4,351
  • 3
  • 37
  • 49
0

"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.

Sezin Karli
  • 2,517
  • 19
  • 24
0

you need to fix the comma

Timestamp ts2 = convertStringToTimestamp("2015-04-17 11:29:49.564");
Mudassar
  • 3,135
  • 17
  • 22
-2
**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;
    }



}
ASP
  • 437
  • 4
  • 11
  • @Jesper it solves the problem with this "Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());" is it so difficult to understand ? – ASP Jun 23 '15 at 14:57
  • But this is not different from what the OP already posted. (Carefully look at the code in the question). – Jesper Jun 23 '15 at 14:58
  • So this works if you input the pattern directly into the dateFormat.parse() However, with your solution, and trying to fit the inputted string still returns the parseException, disregarding of ',' or '.' (comma dot) – jensd Jun 23 '15 at 15:00
  • @jensd I have updated the code ... this code should work you have to be more consistent with ',' and '.' because at format we are specifying ',' – ASP Jun 23 '15 at 15:07
  • Yes this works, also some other submitted examples works as well. Thank you. – jensd Jun 23 '15 at 15:16
  • Awesome mate ... made last edits if you still want to use ',' and '.' ;) – ASP Jun 23 '15 at 15:17