获取特定日期范围
我需要获得从上周三到昨天的日期范围。该节目每周三运行。这是好方法吗?如果该程序由于某种原因在星期二运行怎么办?我该如何让它发挥作用?谢谢。
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -7);
Date transactionBeginDate = calendar.getTime();
calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date transactionEndDate = calendar.getTime();
I need to get a date range starting last Wednesday to yesterday. This program is run on every Wednesday. Is it the good way to do it? What if the program is run on a Tuesday for some reason? How do I make it work? Thanks.
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -7);
Date transactionBeginDate = calendar.getTime();
calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date transactionEndDate = calendar.getTime();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将使用日历上的 DAY_OF_WEEK 值。
类似...
如果您在星期四执行此操作,您最终会得到同一天的开始日期和结束日期。不过,您应该能够根据自己的喜好更好地调整它。
编辑
如果您想获得一整周的时间,无论程序何时运行,那么我认为您想从结束日期开始,然后获取开始日期...
I would use the DAY_OF_WEEK value on the calendar.
Something like ...
If you do this on a Thursday you'll end up getting your start and end dates as the same day. You should be able to better tweak it to your liking though.
edit
If you wanted to get a whole week regardless of when the program is run then I think you want to start with your end date and then get your start date...
您可能想查看 Joda-Time 的 Interval 功能。如果你需要在 Java 中使用日期,你应该硬着头皮现在就学习 Joda-Time。这会让你以后免去很多头痛。
You may want to look at the Interval feature of Joda-Time. If you need to use dates in Java, you should just bite the bullet and learn Joda-Time now. It will save you a ton of headache later.