如何将java.sql.Timestamp增加14天? [英] How do I increment a java.sql.Timestamp by 14 days?

查看:470
本文介绍了如何将java.sql.Timestamp增加14天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它将一个时间戳作为sql选择的开始日期和结束日期的边界,我希望从今年的第一个星期一开始填充一个星期几周的hashmap作为值和周数作为钥匙。我发现很难使用时间戳,我觉得增加86,400,000秒来增加一天并不是很好,因为这并没有考虑闰日,小时,秒。

I have an app that takes a Timestamp as a boundary for the start date and end date of a sql selection, I want to populate a hashmap with weeks this year since the first monday of the year as the values and the week number as the keys. I'm finding it really hard to work with timestamps and I don't feel very good about adding 86,400,000 seconds to it to increment the day, as this doesn't account for the leap days, hours, seconds.

我计划在13天内添加13天,59分59秒,以便我可以按周查找地图中的开始日期作为关键字,然后使用开始日期来获取结束日期。

I plan on adding 13 days 23 hours, 59 minutes and 59 seconds to it so that I can lookup the start date in the map by the week as the key, then use the start date to get the end date.

所以我想尝试这样的事情:

So I'm looking to try to get something like this:

Week  startDate              endDate
1     2011-01-03 00:00:00    2011-01-16 23:59:59
2     2011-01-17 00:00:00    2011-01-30 23:59:59

使用地图中的前两列,最后一列在查看后计算它。如何安全地增加java.sql.Timestamp?

With the first two columns in the Map and the last one being calculated after looking it up. How do I safely increment a java.sql.Timestamp?

推荐答案

值得注意的是,14天并不总是14 * 24 * 3600秒当您有夏令时,这可能会缩短一个小时或更长。从历史上看,它可能比那复杂得多。

It worth noting that 14 days is not always 14 * 24 * 3600 seconds. When you have daylight savings, this can be an hour shorter or longer. Historically it can be much more complex than that.

相反,我建议使用JodaTime或日历来执行时区相关计算。

Instead I would suggest using JodaTime or the Calendar to perform the time zone dependant calculation.

这篇关于如何将java.sql.Timestamp增加14天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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