How can I compare a DatePicker date to the Calendar Date

亡梦爱人 提交于 2019-12-12 00:25:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!