1

I've searched for a long time already and I can't find the answer. My question is simple...

How do you insert the date of today in an sql database with the Date format using java? Here are the codes. (time and date are now null, how do I get them to get the time of now and te date of today )

    String timeNow = null;
    DateFormat dateNow = null;       

    String sql = "INSERT INTO fys.`luggage` (customer_id, description, location, date, time, is_lost, is_handled) VALUES ('"
            + customerId + "', '" + description + "', '" + location + "', '" 
            + dateNow + "', '" + timeNow + "', '" + isLost 
            + "', '" + isHandled + "')";
    db.insertQuery(sql);
}
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
Markvds
  • 137
  • 10
  • possible duplicate of [Equivalent of DateTime.Now in Java?](http://stackoverflow.com/questions/2010284/equivalent-of-datetime-now-in-java) – Mitch Wheat Nov 12 '13 at 22:52

2 Answers2

2

Dates Are Dates, Not Strings

You seem to be wanting to make a String of your date-time. Not necessary. JDBC is aware of the Java data type java.sql.Date which is a thin wrapper around java.util.Date.

See example in another question, Datetime in java or this java2s.com tutorial.

Joda-Time

By the way, if using Joda-Time, you can convert easily between a org.joda.time.DateTime and java.util.Date. Call the toDate() method on DateTime class.

JSR 310 in Java 8

In the future, Java 8 includes a new set of java.time.* classes defined by JSR 310: Date and Time API to supplant the notoriously bad java.util.Date/Calendar classes. These classes are inspired by Joda-Time but are entirely re-architected.

ISO 8601 String Format

If you really want to pass a date-time as string, use format recognized by your database. For example, Postgres accepts ISO 8601 format and others. See "Date/Time Input" section of Date/Time Types page in Postgres doc.

Let Server Set Date-Time

Generally, it's a better idea to let the database engine insert the date-time rather than use your programming language. Most every database system will have various functions you can call to generate the current date-time. The SQL standard specifies a few such functions, but typically a database system offers various others in addition.

For example, Postgres 9 offers several ways to get current date-time.

Be sure to study the documentation. For example, in Postgres you may choose to get the date-time of the moment when the transaction began or you may get the moment when the date-time function is executed (later than the start of transaction). The transaction-start-time is useful when you are recording that time in multiple records and want them to all match on the same time value.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

No need for your variable. Even tough initializing a Date (with time) to now is just creating a default one. new Date();

Use SYSDATE directly. No need to use 2 column either. Because DATE actually holds the time also.

String sql = "INSERT INTO fys.`luggage` (customer_id, description, location, date_time, is_lost, is_handled) VALUES ('"
            + customerId + "', '" + description + "', '" + location + "', SYSDATE, '" + isLost 
            + "', '" + isHandled + "')";
Szymon
  • 42,577
  • 16
  • 96
  • 114
Rouche
  • 254
  • 2
  • 8