2

I want to convert a string to Timestamp. Here is the sample code.

I want to parse the string to Timestamp..

I have tried to convert it like String date format is "18/01/11";

String date = "25/07/2017";
Timestamp ts =Timestamp.valueOf(date);
DateUtil.convertDate(ts.toString());

convertDate:-

SimpleDateFormat inputFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        Date d = null;
        try {
            d = inputFormat.parse(input);
        } catch (ParseException e) {
            System.out.println("Date Format Not Supported");
            e.printStackTrace();
        }
        SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy");

        return outputFormat.format(d).toString();

if I write like that i am getting the following error

Date Format Not Supported
Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
  • 1
    Can you write the actual string down? And there is no sample code! – user1767754 Dec 07 '17 at 05:43
  • 1
    Have you considered using a `SimpleDateFormat` to parse the `String` to a `Date` value? `18/01/11` is not the same as `yyyy-mm-dd` – MadProgrammer Dec 07 '17 at 05:47
  • answer is here you can check if it works [here](https://stackoverflow.com/questions/6510724/how-to-convert-java-string-to-date-object) – Shaamuji Dec 07 '17 at 05:47
  • Please find my edit.. i have added some sample stuff – Naresh Donthula Dec 07 '17 at 05:54
  • what is your input variable value ? – Gusti Arya Dec 07 '17 at 06:00
  • my input is `ts.toString()` – Naresh Donthula Dec 07 '17 at 06:01
  • in my understanding you have a string, then you want to convert this string to Date object? is that correct? – Gusti Arya Dec 07 '17 at 06:10
  • 1
    Check this one https://stackoverflow.com/questions/22856931/converting-java-date-to-sql-timestamp . – Nivethi Mathan Dec 07 '17 at 06:31
  • Why are you going for an instance of the outdated `java.sql.Timestamp` class? These days I recommend the modern replacement, `java.time.Instant`. A modern JDBC driver (JDBC 4.2 and newer) will be happy to store your `Instant` to your database through `PreparedStatement.setObject()`. – Ole V.V. Dec 07 '17 at 06:41
  • Also, if your string holds a date without time of day, what would you use a `Timestamp` for? `java.time.LocalDate` would be the appropriate choice (or `java.sql.Date` if for some reason you insist on the old-fashioned classes). – Ole V.V. Dec 07 '17 at 06:45
  • This question has been asked and answered more than once before. For your own sake, please search before posting a question. – Ole V.V. Dec 07 '17 at 09:16

1 Answers1

0

I think you are using just opposite formats if your input format is dd/MM/yyyy:

//date format of input

SimpleDateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy");
Date d = null;
try {
    //convert string to date
    d = inputFormat.parse(input);
} catch (ParseException e) {
    System.out.println("Date Format Not Supported");
    e.printStackTrace();
}

//output date format

SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//convert date to timestamp

return  outputFormat.format(d).toString();
VPK
  • 3,010
  • 1
  • 28
  • 35
DhaRmvEEr siNgh
  • 1,918
  • 2
  • 13
  • 17