Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While converting String to Date got ambiguity between java.util.Date and java.sql.Date

I am trying to get date as input from Date tag of HTML

Birth Date <input type="date" name="dob"/>

and accessing on jsp page by using

String strDate = request.getParameter("dob");

but it returns in the format of string and I wanted to convert it in to date, I have tried as follows

SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(strDate);

But it gives error

incompatible types required: java.sql.Date found: java.util.Date

and when I imported pakage "java.util.*" for using java.util.Date date = sdf.parse(strDate); it gives

reference to Date is ambiguous, both class java.util.Date in java.util and class java.sql.Date in java.sql match

like image 816
Atish Kumbhar Avatar asked Mar 12 '23 19:03

Atish Kumbhar


2 Answers

If you need a java.sql.Date (and incompatible types required: java.sql.Date found: java.util.Date indicates you do), remove the import of java.util.Date and use the java.util.Date returned by the DateFormat to construct a java.sql.Date with something like

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
    Date date = new Date(sdf.parse(strDate).getTime());
} catch (ParseException e) {
    e.printStackTrace();
}
like image 126
Elliott Frisch Avatar answered Mar 15 '23 08:03

Elliott Frisch


tl;dr

LocalDate.parse( "2018-01-23" )

Details

The Answer by Elliott Frisch is correct. One comment there asked if is there is a better conversion recommended. There is indeed a better way. Bonus: that better way avoids the original problem, confusing java.util.Date with java.sql.Date.

java.time

The java.time framework built into Java 8 and later supplants the poorly-designed troublesome old java.util.Date/.Calendar classes and their kin. The new classes are inspired by the highly successful Joda-Time framework, similar in concept but re-architected. Defined by JSR 310. See the Oracle Tutorial. Extended by the ThreeTen-Extra project. Back-ported to Java 6 & 7 by the ThreeTen-Backport project, which is wrapped for use in Android by the ThreeTenABP project.

The LocalDate class represents a date-only value, without time-of-day and without time zone.

ISO 8601

Your input string happens to comply with the ISO 8601 standard.

This standard is used by default in the java.time classes for parsing/generating date-time strings. So no need to specify a formatting pattern.

LocalDate localDate = LocalDate.parse( "2016-01-23" );

Database

As of JDBC 4.2 and later, you can exchange java.time objects directly with your database via getObject/setObject methods.

JDBC 4.2 and later

Insert/Update.

myPreparedStatement.setObject( … , localDate ) ;

Retrieval.

LocalDate localDate = myResultSet.getObject( … , LocalDate.class ) ;

JDBC 4.1 and earlier

If your JDBC driver is not yet updated for JDBC 4.2 or later, convert between java.time and java.sql types. The old java.sql classes have new methods for such conversions.

For LocalDate, convert to/from the java.sql.Date class (which pretends to be a date-only value).

java.sql.Date mySqlDate = java.sql.Date.valueOf( localDate );

…and going the other direction…

java.time.LocalDate localDate = mySqlDate.toLocalDate();

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?

  • Java SE 8, Java SE 9, Java SE 10, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.

like image 43
Basil Bourque Avatar answered Mar 15 '23 09:03

Basil Bourque