[英]Java date-manipulation
I'm trying to get a date with a Month number, week of month number and a day of week number I thought this will be easy and did this:我试图得到一个带有月号、月号和星期号的日期我认为这很容易并且做到了:
LocalDate nextbookingDate = LocalDate.now().plusYears(1);
nextBookingDate = nextBookingDate.with(Month.of(1));
nextBookingDate = nextBookingDate.with(WeekFields.ISO.weekOfMonth(), 1);
nextBookingDate = nextBookingDate.with(DayOfWeek.of(1));
System.out.println(nextBookingDate); //2019-12-30
nextBookingDate
should be 2020-01-06 because its the first Monday in January. nextBookingDate
应该是 2020-01-06,因为它是一月份的第一个星期一。
But why do I get 2019-12-30 and how do I solve this?但是为什么我会收到 2019-12-30 以及如何解决这个问题?
In each new line, you are overwriting what you have done in the previous line.在每个新行中,您都将覆盖您在前一行中所做的工作。 Try something like this:
尝试这样的事情:
nextBookingDate = now()
.with(Month.of(1))
.with(WeekFields.ISO.weekOfMonth(), 1)
.with(DayOfWeek.of(1));
but be aware that December 30, 2019 is actually the first day of week 1 of 2020.但要注意,二零一九年十二月三十零日实际上是2020年第一周的第一天。
Because the question has been updated, a more relevant answer would be:由于问题已更新,因此更相关的答案是:
LocalDate nextBookingDate = LocalDate.now().plusYears(1)
.with(Month.JANUARY)
.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.MONDAY));
and you can replace the 1 as argument for dayOfWeekInMonth with a number from 2 through 5 as appropriate.并且您可以根据需要将 1 作为 dayOfWeekInMonth 的参数替换为 2 到 5 之间的数字。
It's not completely clear to me what result you want in general, and why.我并不完全清楚您一般想要什么结果,以及为什么。 If I may assume that you want the next date that is an n th some-day-of-week of some month, it's a little more complicated than your code.
如果我可以假设你希望那是个有些天的周某月N的下一个日期,这是一个有点比你的代码变得更复杂。 EDIT: NorthernSky is correct in the comment under his/her answer that
.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.MONDAY))
more directly and briefly gets us what we need.编辑: NorthernSky 在他/她的回答下的评论中是正确的,即
.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.MONDAY))
更直接和简要地为我们提供了我们需要的东西。 This should work:这应该有效:
ZoneId zone = ZoneId.of("Africa/Bamako");
LocalDate today = LocalDate.now(zone);
LocalDate nextBookingDate = today.with(Month.JANUARY)
.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.MONDAY));
if (nextBookingDate.isBefore(today)) {
// Take next year instead
nextBookingDate = today.plusYears(1)
.with(Month.JANUARY)
.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.MONDAY));
}
System.out.println("Next booking date: " + nextBookingDate);
Output when I ran the code just now, was:我刚才运行代码时的输出是:
Next booking date: 2020-01-06
下次预订日期:2020-01-06
TemporalAdjusters.dayOfWeekInMonth()
will get us the 1st Monday, 3rd Tuesday, etc., of the month. TemporalAdjusters.dayOfWeekInMonth()
将为我们提供该月的第一个星期一、第三个星期二等。 So feed any day of week and any number up to 4 or 5 into this method.因此,将一周中的任何一天和最多 4 或 5 的任何数字输入到此方法中。
Please supply your desired time zone where I put Africa/Bamako since it is never the same date in all time zones.请提供您想要的时区,因为它在所有时区中都不是相同的日期。
Link: Documentation of TemporalAdjusters.dayOfWeekInMonth()
.链接:
TemporalAdjusters.dayOfWeekInMonth()
文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.