SimpleDateFormat不显示正确的日期格式 [英] SimpleDateFormat not showing the right date format
问题描述
代码:
SimpleDateFormat sdf =新的SimpleDateFormat(dd-MM-yyyy hh:mm:ss);
sdf.format(rs.getDate(comment_date)); // rs - > ResultSet
从的javadoc java.sql.Date
:
为了符合SQL DATE的定义,通过将实例关联的特定时区中的小时,分钟,秒和毫秒设置为零,必须对java.sql.Date实例进行归一化。
为了保留时间信息,您应该使用 java.sql.Timestamp
。换句话说,将 rs.getDate()
更改为 rs.getTimestamp()
。
I have a DateTime field in my SQL database holding and when I try to show it in my JSP page and format it using "dd-MM-yyyy hh:mm:ss", it displays correctly only the date part. For example, for the date "2012-01-19 12:13:48" stored in the database it shows "19-01-2012 12:00:00". What may be the problem?
Code:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
sdf.format(rs.getDate("comment_date")); //rs -> ResultSet
From the javadoc for java.sql.Date
:
To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
In order to preserve time information as well, you should be using java.sql.Timestamp
. In other words, change rs.getDate()
to rs.getTimestamp()
.
这篇关于SimpleDateFormat不显示正确的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!