gpt4 book ai didi

android - 我如何在每个按钮上按顺序放置日期?

转载 作者:行者123 更新时间:2023-11-29 23:29:57 25 4
gpt4 key购买 nike

我有 7 个按钮。在第一个按钮上我显示当前日期,在第二个按钮上我希望它显示明天的日期,在第三个按钮上显示明天的日期,依此类推。

我使用 Calendar 尝试了几次,但当此 Activity 打开时应用程序关闭。有人可以告诉我如何在我的案例中使用 Calendar 吗?或者如何解决这个问题?

public class OrderActivity extends AppCompatActivity {

Button dateButton1, dateButton2;

Calendar calender = Calendar.getInstance();
Date today = new Date();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);

dateButton1 = (Button)findViewById(R.id.button5);
dateButton2 = (Button)findViewById(R.id.button6);


int amount = 1; // amount of days you want to add to the current date

SimpleDateFormat formattedDate = new SimpleDateFormat("MM");

today.setTime(System.currentTimeMillis()); //set to current date
dateButton1.setText(formattedDate.format(today));


//this code below cause app stoped when this activity start
calender.add(Calendar.DATE, amount);
String newDate = (String)(formattedDate.format(calender.getTime()));

dateButton2.setText(formattedDate.format(newDate));

}}

这是我的最后一个构建,我如何在 dateButton2 上应用下一个日期,在 dateButton3 上应用下一个日期等等?

到目前为止,当进入此 Activity 应用程序关闭时,它在 logcat 上

> java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(DateFormat.java:306)
at java.text.Format.format(Format.java:157)

PS:抱歉英语不好

最佳答案

这应该有效:

public class OrderActivity extends AppCompatActivity {

Button dateButton1, dateButton2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);

Date date = new Date();

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");

LocalDateTime today = LocalDateTime.now();
LocalDate tomorrow = LocalDate.now().plusDays(1);

// LocalDate dayAfterTomorrow = LocalDate.now().plusDays(2);
// continue like this

dateButton1 = (Button)findViewById(R.id.button5);
dateButton2 = (Button)findViewById(R.id.button6);

dateButton1.setText(dtf.format(now)));
dateButton2.setText(dtf.format(tomorrow)));
}

}

关于android - 我如何在每个按钮上按顺序放置日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52833276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com