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

} }

J4C3N-14
  • 686
  • 1
  • 13
  • 32

3 Answers3

3

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());
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • @patrik, thanks, You say I can compare them directly from the datepicker so for example, if I have got the dates from the date picker as: getday, getmonth, getyear etc then how would I compare them? – J4C3N-14 Oct 05 '12 at 12:48
  • ok will post quick example now for getting the datepicker results, changed from the string to standard ints, so basically just need to compare those with the calendar, thanks – J4C3N-14 Oct 05 '12 at 12:59
  • I have posted some code can you give me an example of using the datepicker values directly? thanks – J4C3N-14 Oct 05 '12 at 14:37
2

You can convert your String to Date see this

and try this compareTo

date1.compareTo(date2);
MAC
  • 15,799
  • 8
  • 54
  • 95
0

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

Community
  • 1
  • 1