简体   繁体   English

将String转换为Date时,java.util.Date和java.sql.Date之间存在歧义

[英]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 我试图从HTML的Date标签获取日期作为输入

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

and accessing on jsp page by using 并使用。在jsp页面上访问

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 需要不兼容的类型:找到java.sql.Date:java.util.Date

and when I imported pakage "java.util.*" for using java.util.Date date = sdf.parse(strDate); 当我导入pakage“java.util。*”以使用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 对Date的引用是不明确的,java.util中的类java.util.Date和java.sql中的类java.sql.Date匹配

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 如果需要java.sql.Date (并且需要不兼容的类型:java.sql.Date found:java.util.Date表示你这样做),删除java.util.Date的导入并使用返回的java.util.Date通过DateFormat构造一个类似的java.sql.Date

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
    Date date = new Date(sdf.parse(strDate).getTime());
} catch (ParseException e) {
    e.printStackTrace();
}

tl;dr TL;博士

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

Details 细节

The Answer by Elliott Frisch is correct. Elliott Frisch回答是正确的。 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.util.Datejava.sql.Date混淆。

java.time 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. Java 8及更高版本中内置的java.time框架取代了设计糟糕的旧java.util.Date / .Calendar类及其亲属。 The new classes are inspired by the highly successful Joda-Time framework, similar in concept but re-architected. 新课程的灵感来自非常成功的Joda-Time框架,在概念上类似但重新设计。 Defined by JSR 310 . JSR 310定义。 See the Oracle Tutorial . 请参阅Oracle教程 Extended by the ThreeTen-Extra project. ThreeTen-Extra项目扩展。 Back-ported to Java 6 & 7 by the ThreeTen-Backport project, which is wrapped for use in Android by the ThreeTenABP project. ThreeTen-Backport项目反向移植到Java 6和7,该项目由ThreeTenABP项目包装在Android中使用。

The LocalDate class represents a date-only value, without time-of-day and without time zone. LocalDate类表示仅限日期的值,没有时间和没有时区。

ISO 8601 ISO 8601

Your input string happens to comply with the ISO 8601 standard. 您的输入字符串符合ISO 8601标准。

This standard is used by default in the java.time classes for parsing/generating date-time strings. 默认情况下,此标准在java.time类中用于解析/生成日期时间字符串。 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及更高版本开始,您可以通过getObject / setObject方法直接与数据库交换java.time对象。

JDBC 4.2 and later JDBC 4.2及更高版本

Insert/Update. 插入/更新。

myPreparedStatement.setObject( … , localDate ) ;

Retrieval. 恢复。

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

JDBC 4.1 and earlier JDBC 4.1及更早版本

If your JDBC driver is not yet updated for JDBC 4.2 or later, convert between java.time and java.sql types. 如果尚未针对JDBC 4.2或更高版本更新JDBC驱动程序 ,请在java.time和java.sql类型之间进行转换。 The old java.sql classes have new methods for such conversions. 旧的java.sql类具有此类转换的新方法。

For LocalDate , convert to/from the java.sql.Date class (which pretends to be a date-only value). 对于LocalDate ,转换为/来自java.sql.Date类(假装为仅日期值)。

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

…and going the other direction… ......走向另一个方向......

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

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用符合JDBC 4.2或更高版本的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM