【问题标题】:why LocalDate value did'nt set on today date为什么 LocalDate 值没有在今天设置
【发布时间】:2020-05-07 23:43:32
【问题描述】:

所以我有 7 个不同的按钮,我想在每个 button设置 和当前的 date++ 有序。

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 today = Calendar.getInstance().getTime();
        final SimpleDateFormat fcDate = new SimpleDateFormat("dd");

【问题讨论】:

标签: java android date datetime calendar


【解决方案1】:

使用流

Answer by forpas 是正确的。为了好玩,让我们尝试使用流。我声称这比其他第一个解决方案更好,只是一种替代方案。

我们使用IntStream 数到七。我们使用生成的整数将天数添加到当前日期,并使用其他答案中显示的命名方法访问特定按钮。

我们调用LocalDate::getDayOfMonth,而不是通过DateTimeFormatter 来获取月份中的某天。通过String.valueOf从int转换为字符串。

我没有尝试运行此代码,因为我不从事 Android 工作。但希望它接近工作。

ZoneId z = ZoneId.of( "Asia/Jakarta" ) ;
LocalDate today = LocalDate.now( z ) ;

IntStream
.range( 0 ,  7 )
.forEach( 
    i -> 
    ( 
        (Button) findViewById( 
            getResources().getIdentifier( "hari_" + (i + 1), "id" , getPackageName() )   // See: https://stackoverflow.com/a/59849331/642706  
        ) 
    )                         // Returns a `Button`.
    .setHint ( 
        String.valueOf(       // Convert int (day-of-month) to text.
            today
            .plusDays( i )    // Produces a new `LocalDate` object rather than modifying the original (immutable objects). 
            .getDayOfMonth()  // Returns an `int`. 
        )
    )  
    ;
)
;

【讨论】:

  • 感谢先生的建议。很高兴知道那里案件的另一种方法。真的很感激......
【解决方案2】:

您的代码的问题在于,在循环的 7 次迭代中,您将提示分配给所有按钮,因此您对每个按钮执行 7 次分配,总共 49 次分配,每次分配后增加日期,所以你到达了这些不正确的日期。
所以结果是您看到上次迭代分配的值显然是错误的。
在每次迭代中对 1 个按钮执行 1 个分配,如下所示:

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 并使用这一行:

Button button = (Button) findViewById(buttonId);

你得到一个引用按钮的变量。

【讨论】:

  • 我如何处理 'n' ?这是错误无法解析符号。我应该为此创建新变量 int 吗?
  • 我在button.setHint(date.format(dateFormater)); void android.widget.Button.setHint(java.lang.CharSequence)' 上的空对象引用上出错 日期值为空?为什么?
  • hari1 = (Button)findViewById(R.id.hari_1); xml 中的 id hari_1,hari_2,hari_3,ect.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
  • 2019-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
相关资源
最近更新 更多