问题描述
我有一个应用程序,它以时间戳作为 sql 选择的开始日期和结束日期的边界,我想填充一个哈希图,其中自今年的第一个星期一以来的周数作为值和周数作为钥匙.我发现使用时间戳真的很难,而且我对添加 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 天 23 小时 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 或 Calendar 来执行与时区相关的计算.
Instead I would suggest using JodaTime or the Calendar to perform the time zone dependant calculation.
这篇关于如何将 java.sql.Timestamp 增加 14 天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!