获取特定日期范围

发布于 2024-10-28 05:04:36 字数 359 浏览 1 评论 0原文

我需要获得从上周三到昨天的日期范围。该节目每周三运行。这是好方法吗?如果该程序由于某种原因在星期二运行怎么办?我该如何让它发挥作用?谢谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女 2024-11-04 05:04:36

我将使用日历上的 DAY_OF_WEEK 值。

类似...

while(calendar.DAY_OF_WEEK != Calendar.WEDNESDAY){
    calendar.add(Calendar.DAY_OF_MONTH, -1);
}
Date transactionBeginDate = calendar.getTime();

如果您在星期四执行此操作,您最终会得到同一天的开始日期和结束日期。不过,您应该能够根据自己的喜好更好地调整它。

编辑

如果您想获得一整周的时间,无论程序何时运行,那么我认为您想从结束日期开始,然后获取开始日期...

Calendar calendar = Calendar.getInstance();
//Find last Wednesday to act as end date
while(calendar.DAY_OF_WEEK != Calendar.WEDNESDAY){
    calendar.add(Calendar.DAY_OF_MONTH, -1);
}
Date transactionEndDate = calendar.getTime();
//Go back one week from that Wednesday to get start date.
calendar.add(Calendar.DAY_OF_MONTH, -7);
Date transactionStartDate = calendar.getTime();

I would use the DAY_OF_WEEK value on the calendar.

Something like ...

while(calendar.DAY_OF_WEEK != Calendar.WEDNESDAY){
    calendar.add(Calendar.DAY_OF_MONTH, -1);
}
Date transactionBeginDate = calendar.getTime();

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...

Calendar calendar = Calendar.getInstance();
//Find last Wednesday to act as end date
while(calendar.DAY_OF_WEEK != Calendar.WEDNESDAY){
    calendar.add(Calendar.DAY_OF_MONTH, -1);
}
Date transactionEndDate = calendar.getTime();
//Go back one week from that Wednesday to get start date.
calendar.add(Calendar.DAY_OF_MONTH, -7);
Date transactionStartDate = calendar.getTime();
不交电费瞎发啥光 2024-11-04 05:04:36

您可能想查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文