Textview和String之间的java验证日期
我正在Android上做我的第一个应用程序,它由一个“待办任务”组成。我的问题是当我想检查我的TextView
的文本时,它包含日期(字符串数组),以及保存信息的日期。如果我这样做,运行检查永远不会起作用
// Write date in Array
SimpleDateFormat formatdate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
Calendar calendar = new GregorianCalendar();
String[] date = new String[6];
for(int i = 0; i < 6;i++){
calendar.add(Calendar.DATE , 1);
date[i] = formatdate.format(calendar.getTime());
}
tvdate2.setText(date[0]); // 16-03-2021
...
// When clicking on the button, it checks and then displays the corresponding information
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String test ="16-03-2021";
if (tvdate2.getText().toString() == test){
testing.setText("It works!!");
}
}
});
# 1 楼答案
我已经有一段时间没有使用java了,但是要比较两个字符串,需要使用
.equals()
。您可能想要的是: