如何使用LocalDate添加周数来更改年份?

huangapple go评论86阅读模式
英文:

How can i change year with LocalDate by adding weeks?

问题

以下是翻译好的内容:

这是我的代码。此方法仅适用于1年(当前年份)。当周数超过53时,日期从当前年份的第一周的日期开始,而不是下一周。

一年有52或53周,所以当周数为最后一周(52或53)时,我有2020-12-28至2021-01-03,但当我想获取下一年的第一周(2021-01-04至2021-01-10)时,它输出给我2020年的第一周(2019-12-30至2020-01-05)。

public void showPreviousNextWeekDays(DataBaseHelper dataBaseHelper, long weekChange) {
        if (weekChange >= 0) {
            LocalDate ld = LocalDate.now().plusWeeks(weekChange);

            final long weekNumber = ld.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); // 从当前日期获取周数 

             LocalDate firstDayOfWeek = ld.ofYearDay(ld.getYear(), (int)weekNumber)
                     .with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekNumber)
                     .with(DayOfWeek.MONDAY);

            LocalDate lastDayOfWeek = ld.ofYearDay(ld.getYear(), (int)weekNumber)
                    .with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekNumber)
                    .with(DayOfWeek.SUNDAY);

            String firstAndLastDayOfWeek = firstDayOfWeek.toString() + " - " + lastDayOfWeek.toString();
            daysArrayAdapter = new ArrayAdapter<DayModel>(getActivity(), android.R.layout.simple_list_item_1, db.NextWeek(weekChange));
            daysList.setAdapter(daysArrayAdapter);

如何在年份上继续添加周数呢?

英文:

Here is my code. This method works only for 1 ( current year). When week number is more than 53, dates start from dates in first week in current year but not the next.
Year has 52 or 53 weeks, so when week is last (52or53) i have 2020-12-28 - 2021-01-03, but when I want get first week of next year (2021-01-04 - 2021-01-10) it outputs me first week from 2020 ( 2019-12-30 - 2020-01-05).


public void showPreviousNextWeekDays(DataBaseHelper dataBaseHelper, long weekChange) {
        if (weekChange &gt;= 0) {
            LocalDate ld = LocalDate.now().plusWeeks(weekChange);

            final long weekNumber = ld.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); //takes week from current date 



             LocalDate firstDayOfWeek = ld.ofYearDay(ld.getYear(), (int)weekNumber)
                     .with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekNumber)
                     .with(DayOfWeek.MONDAY);


            LocalDate lastDayOfWeek = ld.ofYearDay(ld.getYear(), (int)weekNumber)
                    .with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekNumber)
                    .with(DayOfWeek.SUNDAY);


            String firstAndLastDayOfWeek = firstDayOfWeek.toString() + &quot; - &quot; + lastDayOfWeek.toString();
            daysArrayAdapter = new ArrayAdapter&lt;DayModel&gt;(getActivity(), android.R.layout.simple_list_item_1, db.NextWeek(weekChange));
            daysList.setAdapter(daysArrayAdapter);

How can i go forward with years also by adding weeks?

答案1

得分: 1

你是对的,你的代码是不正确的。当我将weekChange设置为14并在今天运行(9月29日),得到的结果是2019-12-30 - 2020-01-05,而期望的结果应该是2021-01-04 - 2020-01-10。另外,对于15和16周,你的代码会回到今年年初的一周,而不是找到明年年初的一周,这是不正确的。

我建议以下这个更简单的方法:

int weekChange = 14;

LocalDate ld = LocalDate.now(ZoneId.systemDefault()).plusWeeks(weekChange);
LocalDate firstDayOfWeek = ld.with(DayOfWeek.MONDAY);
LocalDate lastDayOfWeek = ld.with(DayOfWeek.SUNDAY);

String firstAndLastDayOfWeek = firstDayOfWeek.toString() + " - " + lastDayOfWeek.toString();

System.out.println(firstAndLastDayOfWeek);

在今天运行后的输出结果是:

> 2021-01-04 - 2021-01-10

LocalDate使用ISO日历系统,包括ISO周,如果没有明确指示,则可以确保ld.with(DayOfWeek.MONDAY)会给我们当前ISO周的星期一。同样,ld.with(DayOfWeek.SUNDAY)会给我们当前ISO周的星期日。因此,没有更多需要考虑的内容。

你的代码出了什么问题?

weekChange为14,并在今天运行为例。

ld变为2021-01-05(从今天起14周的星期二)。这个日期在2021年的第1周,因此weekNumber为1。

ofYearDayLocalDate静态方法。因此,ld.ofYearDay(ld.getYear(), (int)weekNumber)会得到与LocalDate.ofYearDay(ld.getYear(), (int)weekNumber)相同的日期。它不会使用ld做任何事情。由于weekNumber为1,你会得到2021年的第1天,即2021-01-01。将周数作为年的第几天传递给ofYearDay()是没有意义的。2021-01-01恰好在2020年的第53周。因此,with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekNumber)将日期调整回2020年的第1周。这就是我们错了一年的地方。结果是2020-01-03。那一周的星期一是2019-12-30,这解释了为什么你在输出中得到了这个日期作为一周的第一天。

最后一周的情况类似。周数2和3也类似,因为你将周数用作年的第几天,而1月2日和3日也在前一年2020的最后一周。

我是如何找出的呢?我在我的调试器中运行了你的代码。在那里,问题就很清楚地显示出来了。如果你还没有学会使用调试器,我建议现在是最好的时机。

英文:

You are correct that your code is not correct. When I set weekChange to 14 and run today (September 29), I get 2019-12-30 - 2020-01-05 where expected result would be 2021-01-04 - 2020-01-10. Also for 15 and 16 weeks your code goes back to a week in the beginning of this year rather than finding a week in the beginning of next year as it should.

I suggest the following, which is also a bit simpler:

	int weekChange = 14;

	LocalDate ld = LocalDate.now(ZoneId.systemDefault()).plusWeeks(weekChange);
	LocalDate firstDayOfWeek = ld.with(DayOfWeek.MONDAY);
	LocalDate lastDayOfWeek = ld.with(DayOfWeek.SUNDAY);

	String firstAndLastDayOfWeek = firstDayOfWeek.toString() + &quot; - &quot; + lastDayOfWeek.toString();

	System.out.println(firstAndLastDayOfWeek);

Output when run today:

> 2021-01-04 - 2021-01-10

LocalDate uses the ISO calendar system including ISO weeks if not explicitly instructed otherwise. So we can be sure that ld.with(DayOfWeek.MONDAY) gives us Monday at the start of the same ISO week. Similarly ld.with(DayOfWeek.SUNDAY) gives us Sunday at the end of the ISO week. So there is nothing more to it.

What happened in your code?

Sticking to the case of weekChange 14 and running today.

ld becomes 2021-01-05 (Tuesday 14 weeks from now). This date is in week 1 of 2021, so weekNumber will be 1.

ofYearDay is a static method of LocalDate. So ld.ofYearDay(ld.getYear(), (int)weekNumber) gives you the same date as you would have got from LocalDate.ofYearDay(ld.getYear(), (int)weekNumber). It does not use ld for anything. Since weekNumber is 1, you get day 1 of 2021, so 2021-01-01. Passing the week number as the day of year to ofYearDay() is meaningless. 2021-01-01 happens to fall in week 53 of 2020. So with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekNumber) adjusts the date back to week 1 of 2020. This is where we go a year wrong. The result is 2020-01-03. The Monday of that week is 2019-12-30, which explains why you got this as the first day of week in the output.

The last day of week is similar. And week numbers 2 and 3 are similar since you use the week number as a day of year, and January 2 and 3 fall in the last week of the previous year, 2020, too.

How did I find out? I ran your code in my debugger. There it was clear to see. If you haven’t yet learned to use a debugger. I suggest that now is the best of all times.

huangapple
  • 本文由 发表于 2020年9月28日 23:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64105391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定