Java 在android studio中获取日期选择器日期(年、月、日)的按钮

Java 在android studio中获取日期选择器日期(年、月、日)的按钮,java,android,Java,Android,我想从android studio中的日期选择器中获取类似于(“年mm DD”)的日期,并在按下按钮时将其与当前日期进行比较 public class UserMenu extends ActionBarActivity { DatePicker date; ImageButton next; Date currentDate; @Override protected void onCreate(Bundle savedInstanceState) {

我想从android studio中的日期选择器中获取类似于(“年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);
                }
            }
        });
    }
}
公共类用户菜单扩展了ActionBarActivity{
日期选择器日期;
图像按钮下一步;
日期当前日期;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.user_菜单);
日期=(日期选择器)findViewById(R.id.DatePicker);
next=(ImageButton)findViewById(R.id.nxt);
currentDate=新日期();
next.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
SimpleDataFormat dateformat=新SimpleDataFormat(“yyyy-MM-dd”);
字符串dateFormat=dateFormat.format(新日期(Date.getYear(),Date.getMonth(),Date.getDayOfMonth());
String nowDate=新的SimpleDateFormat(“yyyy-MM-dd”).格式(currentDate);
如果(dateFormat.compareTo(nowDate)<0){
Toast.makeText(getApplicationContext(),“日期太旧”,Toast.LENGTH\u LONG);
}否则{
Intent i=新Intent(UserMenu.this、UserMenuTime.class);
i、 putExtra(“日期”,日期格式);
星触觉(i);
}
}
});
}
}
使用和方法比较日期:

   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);
   }

请张贴您迄今为止尝试过的内容。请告诉我们上述代码存在什么问题