简体   繁体   English

将SQL日期转换为Java.util.date

[英]Convert SQL date to Java.util.date

My requirement is to return the dueDate field as a date to the calling service. 我的要求是将dueDate字段作为日期返回给呼叫服务。

The back end code returns the date in mm/dd/yy . 后端代码以mm/dd/yy返回日期。 When I map those fields to the Java code, my object contains the date field of type java.sql.Date . 当我将这些字段映射到Java代码时,我的对象包含类型为java.sql.Date的date字段。

The field that is defined in the object is (note it's the pseudo): 对象中定义的字段是(请注意是伪字段):

import java.sql.Date;

private Date dueDate;

/**
 * @return the dueDate
 */
public Date getDueDate() {
    return dueDate;
}

/**
 * @param DueDate the DueDate to set
 */
public void setDueDate(Date dueDate) {
    this.dueDate = dueDate;
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("DueDetails [dueAmount=").append(dueAmount).append(", dueDate=").append(dueDate)
            .append("]");       
    return builder.toString();
}

When I print the object, the Date field comes in this form: 当我打印对象时,“ Date字段采用以下形式:

dueDate=Thu Oct 25 20:00:00 EDT 2018
dueDate=Thu Aug 02 20:00:00 EDT 2018

When I query from backend, it shows me the proper format (mm/dd/yy), which I should return to the calling service. 当我从后端查询时,它会向我显示正确的格式(mm / dd / yy),我应该返回到调用服务。

check this 检查这个

//converting java.util.Date to java.sql.Date in Java
java.sql.Date sqlDate = new java.sql.Date(now.getTime());
System.out.println("Converted value of java.sql.Date : " + sqlDate);

//converting java.sql.Date to java.util.Date back
java.util.Date utilDate = new java.util.Date(sqlDate.getTime());
System.out.println("Converted value of java.util.Date : " + utilDate);

from this link: http://www.java67.com/2012/12/how-to-convert-sql-date-to-util-date.html 通过此链接: http : //www.java67.com/2012/12/how-to-convert-sql-date-to-util-date.html

Then you can use SimpleDateFormat to format the date as you like: 然后,您可以使用SimpleDateFormat随意设置日期格式:

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String strdate = simpleDateFormat.format(myJavaDate);
System.out.println(strdate);

java.util.Date vs java.sql.Date java.util.Datejava.sql.Date

Your output looks to be coming from a java.util.Date rather than a java.sql.Date . 您的输出似乎来自java.util.Date而不是java.sql.Date

It is unfortunately easy to mixup the two. 不幸的是,将两者混合很容易。 Besides the Date name, the sql one technically is a subclass of the other util one. 除了Date名称外, sql技术上讲是另一种util的子类。 Beware: This is a hack, and a bad hack at that. 当心:这是一个hack,也是一个糟糕的hack。 The documentation warns us to pretend the two are not in an inheritance relationship. 文档警告我们假装两者不在继承关系中。 But the compiler does not know that. 但是编译器不知道这一点。

Here is some example code showing these two classes and the different behavior of their respective toString method. 这是一些示例代码,显示了这两个类以及它们各自的toString方法的不同行为。

// Legacy classes
System.out.println( "java.sql.Date: " + new java.sql.Date( Instant.now().toEpochMilli() ) ) ;
System.out.println( "java.util.Date: " + new java.util.Date() ) ;

See this code run live at IdeOne.com. 看到此代码在IdeOne.com上实时运行。

java.sql.Date: 2018-07-31 java.sql.Date:2018-07-31

java.util.Date: Tue Jul 31 22:44:14 GMT 2018 java.util.Date:2018年7月31日星期二22:44:14

You can see the java.sql.Date::toString method uses the shorter YYYY-MM-DD format used in SQL environments. 您可以看到java.sql.Date::toString方法使用SQL环境中使用的较短的YYYY-MM-DD格式。 That format also happens to comply with ISO 8601 standard as well, by the way. 顺便说一句,该格式也符合ISO 8601标准。

In contrast, the output you report matches that of java.util.Date::toString . 相反,您报告的输出与java.util.Date::toString的输出匹配。

java.time java.time

The issue is moot. 问题是有争议的。 You are using terrible old date-time classes that were supplanted years ago by the java.time classes. 您使用的是可怕的旧日期时间类,而该类早已java.time类取代。

  • java.sql.Date is replaced by java.time.LocalDate , a date-only only value without a time-of-day and without a time zone. java.sql.Date替换为java.time.LocalDate ,这是仅日期的值,没有日期时间和时区。 (FYI, java.sql.Date pretends to have no time-of-day or time zone but actually has both, as an awkward subclass of java.util.Date .) (仅供参考, java.sql.Date假装没有时间或时区,但实际上两者都具有,这是java.util.Date的笨拙子类。)
  • java.util.Date is replaced by java.time.Instant . java.util.Datejava.time.Instant替换。 Both represent a moment in UTC, though Instant has a finer resolution of nanoseconds instead of milliseconds. 两者都代表UTC的时刻,尽管Instant的分辨率更佳,为纳秒而不是毫秒。 (FYI, java.util.Date actually has a time zone buried deep, inaccessible without getter/setter methods, but used in equals etc. One of many poor design decisions in these legacy classes.) (仅供参考, java.util.Date实际上有一个深层的时区,没有getter / setter方法就无法访问,但是在equals 。这些遗留类中许多糟糕的设计决策之一。)

Here is some code, counterparts to that seen above. 这是一些代码,与上面所看到的相对应。

// Modern classes
System.out.println( "LocalDate.now(): " + LocalDate.now() ;
System.out.println( "Instant.now(): " + Instant.now() ) ;
System.out.println( "ZonedDateTime.now( … ): " + ZonedDateTime.now( ZoneId.of( "Africa/Tunis" ) ) ) ;

See this code run live at IdeOne.com. 看到此代码在IdeOne.com上实时运行。

LocalDate.now(): 2018-07-31 LocalDate.now():2018-07-31

Instant.now(): 2018-07-31T22:57:27.763Z Instant.now():2018-07-31T22:57:27.763Z

ZonedDateTime.now( … ): 2018-07-31T23:57:27.904+01:00[Africa/Tunis] ZonedDateTime.now(…):2018-07-31T23:57:27.904 + 01:00 [非洲/突尼斯]


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