1

how to convert date from "Tue May 08 2018 13:15:00" to "2018-05-08 13:15:00.000" in java, As i have to use it for where clause in custom sql query ex- TO_timestamp('2018-05-08 13:15:00.000', 'YYYY-MM-DD HH24:MI:SS.FF')

Arbitur
  • 38,684
  • 22
  • 91
  • 128
Raj N
  • 249
  • 1
  • 18
  • 1
    what you have tried so far? – Amit Bera May 28 '18 at 11:42
  • 1
    Why do you use a string at all, it opens for security issues? Use a prepared statement with a bind variable instead and setTimestamp; then you don't need to care about formats. – ewramner May 28 '18 at 11:44
  • See also [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2). And use your search engine for many more goodies. Maybe even better: [How to store a Java Instant in a MySQL database](https://stackoverflow.com/questions/47198806/how-to-store-a-java-instant-in-a-mysql-database) and/or [Java ZonedDateTime save in the Database](https://stackoverflow.com/questions/43897326/java-zoneddatetime-save-in-the-database). – Ole V.V. May 28 '18 at 11:50
  • @ewramner , I am getting this kind date from UI to fetch details from DB for specific time period, And we are writing query (JPA native query) how to use this date in where clause of native query – Raj N May 28 '18 at 12:46
  • 1
    If you get the input from a user, even more reason to parse it and use a bind variable or they may sneak in a drop database. See the other question for how to parse. JPA native query supports bind variables. – ewramner May 28 '18 at 12:53
  • I didn’t read through it, but thought it might be helpful: [Dealing with Java's LocalDateTime in JPA](https://www.codesod.com/2017/03/dealing-with-javas-localdatetime-in-jpa.html). – Ole V.V. May 28 '18 at 17:00
  • @OleV.V......there is no solution to convert this "Tue May 08 2018 13:15:00" date in given link. – Raj N May 30 '18 at 10:57
  • @ewramner..i found solution like String s= "May 08 2018 13:30:00"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.SSS"); Date d = new Date(s); simpleDateFormat.format(d); System.out.println(simpleDateFormat.format(d)); – Raj N May 30 '18 at 10:59

3 Answers3

1

I think I have a suggestion to try to resolve your problem...

Note: You may have to configure the Locale of the SimpleDateFormat because of the translation of dates in the String. Otherwise the exception java.text.ParseException will be thrown.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Test {

    public static void main(String[] args) {

        try {
            String dateStr = "Tue May 08 2018 13:15:00";

            SimpleDateFormat sdfBefore = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss", Locale.ENGLISH);
            SimpleDateFormat sdfAfter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

            Date date = sdfBefore.parse(dateStr);

            System.out.println(sdfAfter.format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

}

I hope I've helped.

Denis Jr
  • 346
  • 2
  • 11
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. May 30 '18 at 13:18
  • Ok! This can be a simple solution if someone needs to use it in Java 6 in legacy systems. – Denis Jr May 30 '18 at 14:52
1

First you need to parse your string:

    DateTimeFormatter formatter 
            = DateTimeFormatter.ofPattern("EEE MMM dd uuuu H:mm:ss", Locale.ENGLISH);
    String dateTimeString = "Tue May 08 2018 13:15:00";
    LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
    System.out.println(dateTime);

This prints

2018-05-08T13:15

As has been said in the comments, don’t transfer a string to your database. Assuming you are using at least Java 8 and at least JDBC 4.2 just give the parsed LocalDateTime object to the database through your PreparedStatement, for example:

    PreparedStatement queryStatement = yourDbConnection.prepareStatement(
            "select * from your_table where your_column = ?");
    queryStatement.setObject(1, dateTime);

I am assuming that the source of your string and your database agree about in which time zone the date and time should be interpreted. For most purposes you should prefer to be explicit about time zone.

For anyone reading along and needing a string like 2018-05-08 13:15:00.000 for some other purpose than a database query, the way to obtain this format is through one more formatter:

    DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
    String formattedDateTimeString = dateTime.format(targetFormatter);
    System.out.println(formattedDateTimeString);

This prints

2018-05-08 13:15:00.000

Link: The Java™ Tutorials: Trail: Date Time explaining how to use java.time, the modern Java date and time API.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Test {

    public static void main(String[] args) {

        String dateStr = "Tue May 08 2018 13:15:00";
        DateTimeFormatter formatterFrom = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss");
        LocalDateTime localDate = LocalDateTime.parse(dateStr, formatterFrom);

        DateTimeFormatter formatterTo = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        String localDate1 = formatterTo.format(localDate);

        System.out.println(localDate1); // 2018-05-08 13:15:00.000

    }

}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Raj N
  • 249
  • 1
  • 18