English 中文(简体)
日期:待定
原标题:Date.toString() - sql vs util dates

我需要从<条码>中删除时间。 反对。 这里是我的尝试。

<><>Code>:

System.out.println("date " + dbDate);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("formatter.format(dbDate) " + formatter.format(dbDate));
System.out.println("final " + formatter.parse(formatter.format(dbDate)));

<<>Output>:

date 2011-12-03 23:59:59.0
formatter.format(dbDate) 2011-12-03
final Sat Dec 03 00:00:00 IST 2011

我想到最后日期在<代码>2011-12-03上显示。 但是,在将<代码>改为“编码”之后,该<代码><>>/code>采用不同的格式。 我失踪了。 请提供帮助。

<>Update:

在我的申请中,我有两种不同的方法获得<代码>dbDate。 页: 1

第一问题:dbDate = (java.util.Date)rs.getDate (“EXPIRY_DATE”);

dbDate ,System.out.println(“+ dbDate”); date 2011-12-03

第二点使用<条码>dbDate = rs.gettimestamp(“EXPIRY_DATE”);

dbDate,System.out.println(“+ dbDate”;date 2011-12-03 23:59:59.0

这是我的问题。 由于我想到<条码>到条码>,我没有提及整个问题。

<>Solution:

I did not have choices to avoid java.sql.Date as my application methods have multiple usages. I tried the below and worked,

dbDate = new java.sql.Date(dbDate.getTime());
最佳回答

I 需要从目标日期中删除时间

You can t. The java.util.Date object contains both the date and time. Its toString() is also in a fixed format. If you want to represent it without time to humans, then you need to convert it to a String like as you already did. Or, if you intend to store it in the DB without the time (as the db part in the variable name dbDate suggests), then you need to convert it to java.sql.Date.

preparedStatement.setDate(1, new java.sql.Date(dbDate.getTime()));
// ...

Update as per your update, the ResultSet#getDate() returns an instance of java.sql.Date, not java.util.Date (but it is a subclass of java.util.Date, that s why the unnecessary cast worked; please note that casting is not the same as converting, a real conversion would be new java.util.Date(dbDate.getTime())). As you can read in the javadoc of the toString() method of java.sql.Date, it s indeed in yyyy-MM-dd format.

因此,你的具体问题是,你将<条码>java.sql.Date与<条码>javautil。 日期:,以及你对<代码>java的内务工作进行 mis。 日期:,由<代码>到String(>方法误导。 一切照旧。

Related:

问题回答

如果你想要做的是删除<代码>的时间部分。 日期: 反对:

If you only want to obtain a String representation without the time part of the Date object:

  • You ve got to use SimpleDateFormat.format(). You can t make Date.toString() return a different value, it will always use that pattern. Look at its source code.

When you last call formatter.parse() you get back a Date object; the concatenation then makes an implicit call to Date.toString(): the format returned by this call is the default for the locale set in the JVM. What you must understand is that the Date object has no knowledge of the string representation, internally it s just an aggregate of inte

在我写这封信时,我遇到了同样的问题:

The problem is the date value that is taken from database and passed to the web client is in format yyyy-mm-dd but in the application for the first entry there is not database value so we create date object and passed the value to web client which gives us timestamp value. The value that will be passed to web client must be in date format so SimpleDateFormat is not a good choice for me So from this post ı understand the difference of java.sql.date and java.util.date and then create first object as

Date date = new java.sql.Date(1430454600000L); 

编号yyyyyy-mm-d http://code>toString。

java.time

。 您不能从确定的日期加时间的一类物体中删除一天。

另外,你还在使用麻烦的老班(java.util)。 日期:java.sql.Date,现过时,由java.time级取代。

Instead, use a date-only class for a date-only value. The LocalDate class represents a date-only value without time-of-day and without time zone. The java.sql.Date pretends to do the same, but actually does carry a time of day due to very poor design decision of inheriting from java.util.Date. Avoid java.sql.Date, and use only java.time.LocalDate instead.

You are starting with a java.util.Date object apparently. That represents a point on the timeline in UTC with a resolution in milliseconds. So using that to determine a date requires a time zone. The LocalDate class represents a date-only value without time-of-day and without time zone.

时间区对于确定日期至关重要。 无论何时,日期都因区而异。 例如,在 Paris France 是一个新日,而Montréal 魁北克

如果没有规定时间区,则联合核查机制暗中适用其目前的违约时间区。 这种缺省可能随时改变,因此你的结果可能有所不同。 最好把你所期望/预期的时间区明确定为理由。

www.un.org/chinese/ga/president 从未使用过3-4字母缩略语,例如ESTIST,因为它们是not真正的时区,不是标准化的,甚至不是独一无二的。

ZoneId z = ZoneId.of( "America/Montreal" ) ;  

如果你想要利用金伯利进程目前的缺省时间区,请它作为理由通过。 如果省略,科索沃信托公司目前的违约情况就暗中适用。 更明确的是,因为在任何时间的 运行时间上,任何法律均可在证书内任何对应内容的校对违约。

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

To get a date-only value from your java.util.Date, first convert to its java.time replacement, Instant. To convert back and forth, call new methods added to the old classes.

Instant instant = myJavaUtilDate.toInstant() ;

根据定义,这一价值在万国邮联中。 采用您所希望的时间区(ZoneId)生成<代码>ZonedDatetime。

ZonedDateTime zdt = instant.atZone( z ) ;

Finally, extract your desired LocalDate object from ZonedDateTime.

LocalDate ld = zdt.toLocalDate() ;

从JDBC 4.2到以后,你可以直接与您的数据库交换java.time。 因此无需使用<代码>java.sql。 <代码>java.sql.Date和java.sql.timestamp

myPreparedStatement.setObject( … , ld ) ;

Retrieval。

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

About java.time

java.time Framework is into Java 8 and subsequently. 这些班级取代麻烦的旧的legacyjava.util。 日期<>,rel=“nofollow noretinger”>Calendar , &

Joda-time< Project,现在载于mainance Mode,建议向以下人员迁移:a href=http://docs.ora.com/javase/9/docsa/pi/java/parm/sumer_mage. 班级。

欲了解更多情况,见 rel=“nofollow noreferer”<>>。 并寻找许多例子和解释。 具体定义是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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签