日期最佳实践
我有以下问题。我有一个quartz 作业,每 5 分钟运行一次,并进行 Web 服务调用来检索大对象。此应用程序的目的是它将循环遍历对象中的列表并捕获多个类别的计数。
一个具体要求是为每个类别分配一个任意时段,例如上午 9 点至中午 12 点。如果我们处于该窗口中,则计数将添加到从先前作业继续的滚动计数中,否则将被忽略。
问题。我正在从 .properties
文件加载可配置的开始时间(格式为 9:00am EST)和 int
持续时间(以分钟为单位,即 180)。
问题是处理这个问题的最佳方法是什么。现在,当我的业务类实例化时,我只需将可配置的开始时间转换为long
。我使用Calendar
来计算结束时间的long
值。由于这些都是 1970 年代,当我想知道现在是否在这两个时期之间时,我需要一些肮脏的东西。我将当前的 new Date()
格式化,去掉年份,再次使用 Formatter
来 parse()
1970 年的日期,然后我可以与开始时间和结束时间进行简单的长
比较。
我知道必须有一种不那么丑陋的方法来解决这个问题。有什么想法吗?
I have the following problem. I have a quartz job that runs every 5 minutes and makes a web service call retrieving a large object. The purpose of this application is that it will loop through a List in the object and capture counts from a number of categories.
One specific requirement is that each category be assigned an arbitrary window, say 9am-12pm. If we are in that window the counts get added to a rolling count continued from previous jobs, if not it is ignored.
The questions. I am loading the configurable start time(in the format 9:00am EST) and an int
duration(in minutes i.e. 180) from a .properties
file.
The question is what is the best way to handle this. Right now I just convert the configurable start time to long
when my business class is instantiated. I use Calendar
to calculate the long
value of the end time. Since these are both in 1970 time, When I want to find out if now is between those two times it requires some nastiness on my part. I take the current new Date()
format it, strip out the year, use the Formatter
again to parse()
a date in 1970 then I can do a simple long
comparison with my start and end time.
I know there has to be a less ugly way to approach this. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想看看 Joda time (http://joda-time.sourceforge.net/),它包含一个更合理的 API 来处理时间。
You might want to look at Joda time (http://joda-time.sourceforge.net/), which contains a more sane API to deal with time.