如何将 java.sql.Timestamp 增加 14 天?

How do I increment a java.sql.Timestamp by 14 days?(如何将 java.sql.Timestamp 增加 14 天?)
本文介绍了如何将 java.sql.Timestamp 增加 14 天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个应用程序,它以时间戳作为 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 天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)