-2

I am trying to generate the date of today. But I always get: "1969-12-31" as the output.

here my code:

java.util.Date d1 = new java.util.Date();

java.sql.Date sqlDate = new java.sql.Date(d1.getDate());

I want to add it in a sql table, what am I doing wrong?

Any suggestions?

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • The [JavaDocs for `java.util.Date#getDate`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#getDate--) says *"Returns the day of the month represented by this Date object. The value returned is between 1 and 31 representing the day of the month that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone."*, apart from also been depreciated, this clearly is not the method you want to use – MadProgrammer Sep 23 '15 at 11:41

5 Answers5

3

d1.getDate() will give an int representing the current date. Eg: 23.

What you need is d1.getTime().

java.sql.Date sqlDate = new java.sql.Date(d1.getTime());
Codebender
  • 14,221
  • 7
  • 48
  • 85
1
java.sql.Date date = new java.sql.Date(Calendar.getInstance().getTime().getTime());
Bruno Caceiro
  • 7,035
  • 1
  • 26
  • 45
1

java.time

In Java 8 and later you should make use of the new java.time framework. These new classes supplant the old classes which have proven so troublesome.

Time Zone

Note that time zone is crucial in determining a date.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate localDate = LocalDate.now( ZoneId );
java.sql.Date d = java.sql.Date.valueOf( localDate );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

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

The getDate() function is deprecated.

Try use this:

java.util.Date d1 = new java.util.Date();

java.sql.Date sqlDate = new java.sql.Date(d1.getTime());
Majid Shamkhani
  • 849
  • 2
  • 8
  • 28
-1

java.util.date.getDate documentation says that method is deprecated and time that it is returning is actually a base epoch time which begins from 1970-01-01.Learn more about it on https://en.wikipedia.org/wiki/Unix_time

In order to get the actual date you need to do the following:

java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
Vicent
  • 5,322
  • 2
  • 28
  • 36