为什么在今天日期未设置LocalDate值

为什么在今天日期未设置LocalDate值

本文介绍了为什么在今天日期未设置LocalDate值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有7个不同的按钮,我想在每个按钮上使用当前 date ++ sethint

so i have 7 different button, i want to sethint on each button with current date++ orderly.

DateTimeFormatter dateFormater = DateTimeFormatter.ofPattern("d");
        ZoneId zone = ZoneId.of("Asia/Jakarta");
        LocalDate date = LocalDate.now(zone);
        int amount = 1;
        int buttonCount = 7;
        for (int i = 0; i < buttonCount; i++){
            hari1.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari2.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari3.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari4.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari5.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari6.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari7.setHint(date.format(dateFormater));
        }

日期输出开始 27-28-29 等。这是错误的,因为今天的日期是 22 。输出应该是 22-23-24 正确吗?所以我尝试在日期上使用日历,并且输出正确 22 。为什么呢?并且有解决方案,所以我可以得到正确的日期并得到它(日期++)?还有另一种方法吗?

date output start 27-28-29 ect. which is wrong becuse today date is 22. the output should be 22-23-24 ect right ? so i tried using calendar on date and the output is correct 22. why ? and is there solution so i can get the correct date and got it (date++) ? and is there another approach ? how ?

        Date today = Calendar.getInstance().getTime();
        final SimpleDateFormat fcDate = new SimpleDateFormat("dd");


推荐答案

您的代码存在的问题是循环的7次迭代将为所有按钮分配提示,因此您将对每个按钮进行7个分配,共进行49个分配,增加每个分配后的日期,以达到这些错误的日期。

因此,结果是您看到从上次迭代分配的值显然是错误的。

在每次迭代中将1分配给1个按钮,如下所示:

The problem with your code is that in each of the 7 iterations of the loop you are assigning the hints to all the buttons, so you are doing 7 assignments to each button, total 49 assignments, increasing the date after each assignment, so you reach these incorrect dates.
So the result is that you see the values assigned from the last iteration which are obviously wrong.
Do 1 assignment to 1 button in each iteration like this:

DateTimeFormatter dateFormater = DateTimeFormatter.ofPattern("d");
ZoneId zone = ZoneId.of("Asia/Jakarta");
LocalDate date = LocalDate.now(zone);
int amount = 1;
int buttonCount = 7;
for (int i = 0; i < buttonCount; i++){
    int buttonId = getResources().getIdentifier("hari_" + (i + 1), "id", getPackageName()); 
    Button button = (Button) findViewById(buttonId);
    button.setHint(date.format(dateFormater));
    date = date.plusDays(amount);
}

使用此行:

int buttonId = getResources().getIdentifier("hari_" + (i + 1), "id", getPackageName());

您将获得每个按钮的整数 id 并使用以下行:

you get the integer id of each button and with this line:

Button button = (Button) findViewById(buttonId);

您将获得一个引用按钮的变量。

you get a variable referencing the button.

这篇关于为什么在今天日期未设置LocalDate值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 22:30