Java中Calendar实例的时间部分重置
有Java代码,它在2个不同的环境中运行,并使用JdbcTemplate
将记录插入到DB。
两个环境的运行结果都不同。特别是对于 Date
字段。
在第一个环境(Oracle XE)上,它会生成记录:
"12/03/2010";191094;"71697211000";3229;880323202;NULL;0;1;0;NULL;0;NULL
第二个环境(Oracle 非 XE):
"12/03/2010 12:00:00";191094;"71697211000";3229;880323202;NULL;0;1;0;NULL;0;NULL
NLS_DATE_FORMAT(如果很重要)第一个环境是 DD-MON- RR, 第二个环境是 DD-MON-RRRR
问题是,Oracle 设置可能会影响第二个环境日期格式是另一种吗?
There is Java code,which is ran on 2 different environments and inserts records with JdbcTemplate
to DB.
Results of its running are different for both envs. Particularly,for Date
fields.
On first environment(Oracle XE) it produces record:
"12/03/2010";191094;"71697211000";3229;880323202;NULL;0;1;0;NULL;0;NULL
Second environment(Oracle non XE):
"12/03/2010 12:00:00";191094;"71697211000";3229;880323202;NULL;0;1;0;NULL;0;NULL
NLS_DATE_FORMAT(if it's crucial) for first env is DD-MON-RR
,
for second env is DD-MON-RRRR
The question is,what Oracle settings may affect,that second env Date format is another?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
应在 Java 代码中设置以下日历属性:
而不是:
should set following Calendar properties in Java code:
instead of:
根据文档,
HH
指的是12小时时间。您检索的时间中的 12 点是午夜 12 点。您想要的是HH24
,它为您提供 24 小时时间,从午夜 00 点开始。According to the documentation,
HH
refers to a 12-hour time. The 12 in the time you're retrieving is 12 midnight. What you want isHH24
, which ges you a 24-hour time, starting at 00 for midnight.如果您不想显示时间部分,请不要包含包含时间部分的格式字符串(“HH:MI:SS”)。
您已将时间部分重置为午夜,基本上...无法区分设置为午夜的
日历
或日期
与日历
code> 或Date
“没有”时间部分 - 因为不存在仅包含日期部分的Calendar
/Date
这样的概念。现在,您可能可以将其存储在数据库中,具体取决于您可以使用的类型 - 但 java.util.Date 和 java.util.Calendar 始终表示点及时,而不仅仅是日期。
它显示 12 而不是 00 的原因是因为您使用的是“HH”而不是“HH24”,根据 lacqui 的回答。我假设您根本不想看到时间,因为您已将其重置为午夜......
If you don't want to show the time part, don't include a format string which includes the time part ("HH:MI:SS").
You've reset the time part to midnight, basically... there's no way of differentiating between a
Calendar
orDate
set to exactly midnight and aCalendar
orDate
"without" a time part - because there's no such concept as aCalendar
/Date
with only a date part.Now you may be able to have that in the database, depending on what types are available to you - but
java.util.Date
andjava.util.Calendar
always represent points in time, not just dates.The reason it's showing 12 instead of 00 is because you're using "HH" instead of "HH24", as per lacqui's answer. I assume you don't really want to see the time at all though, given that you'll have reset it to midnight...
我建议您扩展日历类之一,如下所示:
以这种方式实例化:
I'd recommend you extend one of the Calendar classes like this:
Instantiate in this way: