问题
A simple question I hope. How can I compare the date entered into a DatePicker to the current Calendar date?
To explain, If I have the DatePicker date stored as a string, how could I compare that to the current date on the android system calendar?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dpresult);
textView1 = (TextView)findViewById(R.id.textView1);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(datePickerResult.this, calander.class);
startActivity(intent);
}
});
Bundle extras = getIntent().getExtras();
year = extras.getInt("year");
day = extras.getInt("day");
month = extras.getInt("month");
textView1.setText(day+"");
} }
回答1:
for example you got the date like string from datepicker then
String sDate = "05-10-2012"; // suppose you create this type of date as string then
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(sDate);
Calendar c = Calendar.getInstance();
c.getTime().compareTo(date);
it depending on your string or how you can get? you can get all individually from datepicker then directly set in calendar instance
Calendar my = Calendar.getInstance();
my.set(year, month, day);
now compare
my.compareTo(Calendar.getInstance());
回答2:
You can convert your String to Date see this
and try this compareTo
date1.compareTo(date2);
回答3:
read through a tutorial on dates here: http://www.mkyong.com/android/android-date-picker-example/
and this post on SO for a similar case: How to match or compare a date string with the date stored in sqlite in android?
If none of this helps then post some code and i'll edit my answer :)
来源:https://stackoverflow.com/questions/12746156/how-can-i-compare-a-datepicker-date-to-the-calendar-date