SimpleDateFormat不显示正确的日期格式 [英] SimpleDateFormat not showing the right date format

查看:435
本文介绍了SimpleDateFormat不显示正确的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SQL数据库中有一个DateTime字段,当我尝试在我的JSP页面中显示它并使用dd-MM-yyyy hh:mm:ss进行格式化时,它只显示日期部分。例如,存储在数据库中的日期2012-01-19 12:13:48显示19-01-2012 12:00:00。可能是什么问题?



代码:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆