如何将 java.sql.Timestamp 添加 14 天?
我有一个应用程序,它采用时间戳作为 sql 选择的开始日期和结束日期的边界,我想用今年第一个星期一以来的周数作为值和周数作为键来填充哈希图。我发现使用时间戳真的很难,而且我不太愿意在其中添加 86,400,000 秒来增加日期,因为这没有考虑到闰日、小时、秒。
我计划向其添加 13 天 23 小时 59 分 59 秒,以便我可以按周作为键在地图中查找开始日期,然后使用开始日期获取结束日期。
所以我希望尝试得到这样的结果:
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?
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.
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这将正确满足您的默认时区中的夏令时转换。如果需要,您可以告诉 Calendar 类使用不同的时区。
This will correctly cater for daylight-time transitions in your default Timezone. You can tell the Calendar class to use a different Timezone if need be.
值得注意的是,14 天并不总是 14 * 24 * 3600 秒。如果有夏令时,这个时间可能会缩短或更长一个小时。从历史上看,情况可能比这复杂得多。
相反,我建议使用 JodaTime 或 Calendar 来执行时区相关计算。
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.
Instead I would suggest using JodaTime or the Calendar to perform the time zone dependant calculation.
爪哇8
Java 8