5
public static void main(String[] args) {
    java.util.Date utilDate = new java.util.Date();
    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
    System.out.println("utilDate:" + utilDate);
    System.out.println("sqlDate:" + sqlDate);
 }

I use this code part and output like this:

2013/06/14 12:06:11
2013-06-14

I want the output which has date and time data. The parameter of method needs java.sql.Date format. that's why I must convert or change format of sqlDate. How can I solve it?

NOTE

PreparedStatement insertStmt=null;
insertStmt.setDate(parIndex, java.sql.Date);

That's why I want java.sql.Date format

kamal
  • 1,093
  • 6
  • 19
  • 34
  • Try `DateFormat.getDateTimeFormat().format(sqlDate)` and see what you get – MadProgrammer Jun 14 '13 at 07:13
  • That shouldn't be the output of your code. java.util.Date.toString() formats the date in a different way. – Bhesh Gurung Jun 14 '13 at 07:15
  • You already have a `Date` which can be formatted using `SimpleDateFormat`, you build your SQL date out of it; by curiosity, why do you want your SQL date formatted too? No trust in `new java.sql.Date()`? – fge Jun 14 '13 at 07:15
  • 1
    The `java.sql.Date(long)` constructor sets the time-of-day components to zero GMT. Formatting it as a `java.util.Date` will be fairly misleading. See http://docs.oracle.com/javase/7/docs/api/java/sql/Date.html#Date(long). – Eric Jablow Jun 14 '13 at 07:22
  • You need to understand the basics of these two data types. See: [java.util.Date vs java.sql.Date](http://stackoverflow.com/q/2305973/642706). – Basil Bourque Jul 18 '15 at 04:07
  • possible duplicate of [How to convert java.sql.Date to java.util.Date in dd/MM/yyyy format?](http://stackoverflow.com/questions/14344373/how-to-convert-java-sql-date-to-java-util-date-in-dd-mm-yyyy-format) – Basil Bourque Jul 18 '15 at 04:08

3 Answers3

6

java.sql.Date extends java.util.Date so it has the same information but displays it differently because of overridden toString() method.

If you do something like this you will see it is the same

System.out.println(new java.util.Date(sqlDate.getTime()));

It is advised to use DateFormat or SimpleDateFormat to display data, see the comments.

stivlo
  • 83,644
  • 31
  • 142
  • 199
Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
3

Use time stamp

    Date updated_date=new Date();
    Timestamp timestamp = new Timestamp(updated_date.getTime());
    updated_date = timestamp;

and make sure Data type in Database should be Timestamp or Datetime

KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
1
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * java.util.date to java.sql.date
 */
public class DatesConversion {

    public static void main(String[] args) {
        java.util.Date uDate = new java.util.Date();
        System.out.println("Time in java.util.Date is : " + uDate);
        java.sql.Date sDate = convertUtilToSql(uDate);
        System.out.println("Time in java.sql.Date is : " + sDate);
        DateFormat df = new SimpleDateFormat("dd/MM/YYYY - hh:mm:ss");
        System.out.println("Using a dateFormat date is : " + df.format(uDate));
    }

    private static java.sql.Date convertUtilToSql(java.util.Date uDate) {
        java.sql.Date sDate = new java.sql.Date(uDate.getTime());
        return sDate;
    }
}

Use this

A.K.
  • 2,284
  • 3
  • 26
  • 35