0
try{
             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
                Date parsedDate = dateFormat.parse(tm);
                timestamp = new java.sql.Timestamp(parsedDate.getTime());

        }
         catch (Exception pe) {
              System.out.println("unexpected exception");
                System.out.println(pe); 
                pe.printStackTrace(); 
            }

Console shows "unexpected error"(which is in my catch block). I am trying to convert "tm" which is client-side input date into a timestamp. The value of tm is like "YYYY-MM-DD HH:MM: SS"(String). But the console keeps on showing Unparseable date. I am new to Java.

7
  • 3
    What's the value of the tm variable? Perhaps it doesn't match your format. Commented Mar 8, 2019 at 18:03
  • It is like "YYYY-MM-DD HH:MM: SS" which the user fill through html page Commented Mar 8, 2019 at 18:05
  • The value in tm must be of String type and the format must be a valid one. Else this throw a run-time exception. Commented Mar 8, 2019 at 18:06
  • @SarthakSrivastava your problem is here "hh" most likely. This is 1-12, not 0-23. Use "HH" Commented Mar 8, 2019 at 18:12
  • Still it is showing unparseable date Commented Mar 8, 2019 at 19:25

2 Answers 2

0

java.time

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");

    String tm = "2019-02-09 07:35:54";
    LocalDateTime dateTime = LocalDateTime.parse(tm, formatter);
    System.out.println(dateTime);

This produces the following output:

2019-02-09T07:35:54

I discourage the use of SimpleDateFormat, Date and Timestamp. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead I am using java.time, the modern Java date and time API.

JDBC 4.2+

If you thought you needed a Timestamp for your database, you probably don’t. Assuming your JDBC driver is up to JDBC 4.2 you can directly give it the LocalDateTime object we just produced. For example like:

    PreparedStatement ps = yourConnection.prepareStatement(
            "insert into your_table(your_timestamp_col) values (?);");
    ps.setObject(1, dateTime);

What went wrong in your code?

There are two errors in your format pattern string, yyyy-MM-dd hh:mm:ss.SSS:

  1. Assuming that the hours in the user input are hour of day from 00 through 23, you need uppercase HH to parse them, as LppEdd already said in a comment (lowercase hh is for hour within AM or PM from 01 through 12 and of course requires that an AM or PM marker is present).
  2. Since the user input doesn’t include fraction of second, you should not have .SSS in the format pattern (this is probably what caused your exception).

Link

Oracle tutorial: Date Time explaining how to use java.time.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you want to try this out

    String tm = "2014-01-01 00:00:00";
    String frontEndFormat = "yyyy-MM-dd HH:mm:ss";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(frontEndFormat);
    LocalDateTime localDateTime = LocalDateTime.parse(tm, formatter);
    Timestamp timestamp = Timestamp.valueOf(localDateTime);
    //prints 2014-01-01 00:00:00.0
    System.out.println(timestamp);

You can notice the upper case HH used for hours and the lower case mm used for minutes.

1 Comment

If one indispensably needs on old-fashioned Timestamp for a legacy API that cannot be changed (just now), this is the right solution. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.