0

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+"");

} }

4

3 回答 3

3

例如,您从 datepicker 获得了类似日期的字符串

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

它取决于您的字符串或您如何获得?您可以从日期选择器中单独获取所有内容,然后直接在日历实例中设置

Calendar my = Calendar.getInstance();
my.set(year, month, day);

现在比较

my.compareTo(Calendar.getInstance());
于 2012-10-05T12:32:31.960 回答
2

您可以将您的字符串转换为日期看到这个

试试这个 compareTo

date1.compareTo(date2);
于 2012-10-05T12:24:55.537 回答
0

在此处阅读有关日期的教程:http ://www.mkyong.com/android/android-date-picker-example/

和这篇关于类似情况的帖子:How to match or compare a date string with the date stored in sqlite in android?

如果这些都没有帮助,那么发布一些代码,我会编辑我的答案:)

于 2012-10-05T12:24:00.497 回答