Lokesh Reddy
Lokesh Reddy

Reputation: 29

How to convert datetime format in sql to datetime in java[eclipse]

I am trying to retrieve values from a database.

In the database I have a column called trip_time which is in date time format "2014-10-13 08:30:00".

I am not able to set and get the values for trip_time in eclipse.

What is the similar format in java which matches with database?

In the database it is in datetime format 2014-10-13 08:30:00. Now currently in eclipse i am using String format for the column trip_time. How do i convert it to datetime format as in sql database?

Upvotes: 1

Views: 1764

Answers (2)

A4L
A4L

Reputation: 17595

Now currently in eclipse i am using String format for the column trip_time.

No this is not what you want to do, this is not what you should do!

Please use PreparedStatement to get and set values for SQL-Statements. For date fields just use the setDate method and pass it a java.sql.Date, It will take care of everything for you.

Please refer to oracle's tutorials on this topic.

Here another one with code example for date fileds.

Upvotes: 0

Sireesh Vattikuti
Sireesh Vattikuti

Reputation: 1190

public class DateTime {

public static void main(String[] args) {
    // Get the value from database, using the steps to connect and get the value


    java.sql.Date trip_time= //"{Collected value from DB}"; 
    //"2014-10-13 08:30:00"; //Assume u got this value

    //Now it's time to convert to Sql date time to java data time

    java.util.Date utilDate = new java.util.Date(trip_time.getTime());
    System.out.println("Converted value of java.util.Date : " + utilDate);  

}

Upvotes: 1

Related Questions