我有7个按钮。在第一个按钮上,我显示当前日期,在第二个按钮上,我希望它显示明天的日期,在第三个按钮上,它想要显示的日期,依此类推。
我使用Calendar
尝试了几次,但是此活动打开时应用程序关闭。有人可以告诉我如何在案件中使用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上应用下一个日期,依此类推?
到目前为止,进入此活动应用程序已关闭且位于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:对不起,英语不好
答案 0 :(得分:1)
这应该有效:
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)));
}
}
答案 1 :(得分:0)
您使用的格式错误。您的格式只有几个月。但是,您可以使用以下代码来增加天数。
Date today = new Date();
int amount = 1; // amount of days you want to add to the current date
SimpleDateFormat formattedDate = new SimpleDateFormat("yyyy-MM-dd");
Calendar calender = Calendar.getInstance();
// add to the current date
calender.add(Calendar.DATE, amount);
String newDate = (String)(formattedDate.format(calender.getTime()));
System.out.println("newDate date is " + newDate);
答案 2 :(得分:0)
您能帮我使用threetenABP吗,但请使用我的案例?
这应该为您提供正确的方向:
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("M-d");
ZoneId zone = ZoneId.of("Asia/Jayapura");
LocalDate date = LocalDate.now(zone);
int amount = 1; // amount of days you want to add to the current date
int buttonCount = 7;
for (int i = 0; i < buttonCount; i++) {
System.out.println(date.format(dateFormatter));
date = date.plusDays(amount);
}
我刚才运行代码时的输出:
10-17
10-18
10-19
10-20
10-21
10-22
10-23
我的进口货是:
import org.threeten.bp.LocalDate;
import org.threeten.bp.ZoneId;
import org.threeten.bp.format.DateTimeFormatter;
您尝试过:
String newDate = (String)(formattedDate.format(calender.getTime()));
dateButton2.setText(formattedDate.format(newDate));
newDate
已被格式化为String
,因此再做一次没有意义。 format
方法接受Date
或Long
,但不接受String
。
java.time
。java.time
。java.time
向Java 6和7(JSR-310的ThreeTen)的反向端口。