简体   繁体   English

Java Calendar奇怪的行为

[英]Java Calendar strange behavior

I'm trying to use a java calendar on which I'm trying to compute the next 7 days after a specific date as following: 我正在尝试使用Java calendar在该calendar上尝试计算特定日期后的未来7天,如下所示:

java.sql.Timestamp lastDate= Timestamp.valueOf("2015-12-28 00:00:00.0");

I have a batch job that runs and updates that date to occur weekly. 我有一个批处理作业,该作业每周运行并更新一次该日期。 The way I did it is as follows : 我的方法如下:

Calendar calNextDate = Calendar.getInstance();
calNextDate.setTime(new java.util.Date(lastDate.getTime()));

calNextDate.add(Calendar.DAY_OF_MONTH, 7);

System.out.println(new Timestamp(calNextDate.getTime().getTime()));

Output : 输出:

2016-01-04 00:00:00.0

but when I use the same function to compute for the year 2014 the date is: 但是当我使用相同的函数计算2014年时,日期为:

2015-01-05 00:00:00.0

I have added 7 days on both the cases but in case of 2014, 1 day is off, why would that be?? 在这两种情况下,我都添加了7天,但是如果2014年是1天,那么为什么呢? thanks in advance. 提前致谢。

Problem Not Reproducible 问题无法重现

I do not see the problem. 我看不到问题。

Here is a re-creation of the code you described but did not post. 这是您描述但未发布的代码的重新创建。

java.sql.Timestamp ts2013 = Timestamp.valueOf ( "2013-12-28 00:00:00.0" );
java.sql.Timestamp ts2014 = Timestamp.valueOf ( "2014-12-28 00:00:00.0" );
java.sql.Timestamp ts2015 = Timestamp.valueOf ( "2015-12-28 00:00:00.0" );

Calendar cal2013 = Calendar.getInstance ( );
cal2013.setTime ( new java.util.Date ( ts2013.getTime ( ) ) );
cal2013.add ( Calendar.DAY_OF_MONTH, 7 );
java.sql.Timestamp ts2013Plus = new Timestamp ( cal2013.getTime ( ).getTime ( ) );
System.out.println ( ts2013 + " + 7 = " + ts2013Plus );

Calendar cal2014 = Calendar.getInstance ( );
cal2014.setTime ( new java.util.Date ( ts2014.getTime ( ) ) );
cal2014.add ( Calendar.DAY_OF_MONTH, 7 );
java.sql.Timestamp ts2014Plus = new Timestamp ( cal2014.getTime ( ).getTime ( ) );
System.out.println ( ts2014 + " + 7 = " + ts2014Plus );

Calendar cal2015 = Calendar.getInstance ( );
cal2015.setTime ( new java.util.Date ( ts2015.getTime ( ) ) );
cal2015.add ( Calendar.DAY_OF_MONTH, 7 );
java.sql.Timestamp ts2015Plus = new Timestamp ( cal2015.getTime ( ).getTime ( ) );
System.out.println ( ts2015 + " + 7 = " + ts2015Plus );

When run, this code always get the 4th. 运行时,此代码始终排在第四位。

2013-12-28 00:00:00.0 + 7 = 2014-01-04 00:00:00.0
2014-12-28 00:00:00.0 + 7 = 2015-01-04 00:00:00.0
2015-12-28 00:00:00.0 + 7 = 2016-01-04 00:00:00.0

I conclude that your problem lies elsewhere. 我认为您的问题出在其他地方。

java.time java.time

I strongly suggest moving to use of the new java.time package in Java 8 and later. 我强烈建议在Java 8及更高版本中使用新的java.time包。 You can then entirely avoid the mess that is java.util.Date/.Calendar. 然后,您可以完全避免java.util.Date/.Calendar的混乱。

The java.sql.Timestamp class has been enhanced with new methods to easily convert to and from java.time types. 通过新方法增强了java.sql.Timestamp类,可以轻松地在java.time类型之间进行转换。

Eventually JDBC drivers will be updated to directly handle the java.time data types. 最终,JDBC驱动程序将被更新以直接处理java.time数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM