我想从android studio中的日期选择器中获取看起来像那个(“Year-mm-DD”)的日期,并在按下按钮时将其与当前日期进行比较。
public class UserMenu extends ActionBarActivity {
DatePicker date;
ImageButton next;
Date currentDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_menu);
date = (DatePicker) findViewById(R.id.datePicker);
next = (ImageButton) findViewById(R.id.nxt);
currentDate = new Date();
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
String dateFormat = dateformat.format(new Date(date.getYear(), date.getMonth(), date.getDayOfMonth()));
String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(currentDate);
if (dateFormat.compareTo(nowDate) < 0) {
Toast.makeText(getApplicationContext(), "The date is too old", Toast.LENGTH_LONG);
} else {
Intent i = new Intent(UserMenu.this, UserMenuTime.class);
i.putExtra("date", dateFormat);
startActivity(i);
}
}
});
}
}
答案 0 :(得分:0)
Date pickerDate = new Date(date.getYear(), date.getMonth(), date.getDayOfMonth());
if (pickerDate.before(currentDate)) {
Toast.makeText(getApplicationContext(), "The date is too old", Toast.LENGTH_LONG);
} else {
Intent i = new Intent(UserMenu.this, UserMenuTime.class);
i.putExtra("date", dateFormat);
startActivity(i);
}