How to compare two dates in String format in Java in Android

Jyotishgher Astrology
By -
0

 How to compare two dates in String format in Java in Android


The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.

  • One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. 
  • To parse/convert a string as a Date object Instantiate this class by passing desired format string.
  • Parse the date string using the parse() method.
  • The util.Date class represents a specific instant time This class provides various methods such as before(), after() and, equals() to compare two dates

Example

Once you create date objects from strings you can compare them using either of these methods as shown below −


 frmdt = (Button) findViewById(R.id.frmdt);//suppose this is edit box For From Date
frmdt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);

cal.add(Calendar.DATE, 0);

DatePickerDialog dialog = new DatePickerDialog(
apply.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener1,
year, month, day);

//Set Minimum date of calendar
// cal.set(2022, 1, 1);


long now = System.currentTimeMillis() - 1000;
dialog.getDatePicker().setMinDate(now);
dialog.getDatePicker().setMaxDate(now + (1000 * 60 * 60 * 24 * 7)); //After 7 Days from Now

// dialog.getDatePicker().setMinDate(cal.getTimeInMillis());
Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
Log.d(TAG, "onDateSet: dd/mm/yyy: " + day + "/" + month + "/" + year);

String date = day + "/" + month + "/" + year;
txtfrom_dt.setText(date);
String From_Date=date;
            }
};
todt = (Button) findViewById(R.id.todt);//suppose this is edit box For To Date
todt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog = new DatePickerDialog(
apply.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year, month, day);

long now = System.currentTimeMillis() - 1000;
dialog.getDatePicker().setMinDate(now);
dialog.getDatePicker().setMaxDate(now + (1000 * 60 * 60 * 24 * 15)); //After 15 Days from Now
Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
Log.d(TAG, "onDateSet: dd/mm/yyy: " + day + "/" + month + "/" + year);

String date = day + "/" + month + "/" + year;
txtto_dt.setText(date);

String To_Date=date;

//creating calendar instances for date comparision
SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");
//Parsing the given String to Date object
Date date1 = null;
try {
date1 = formatter.parse(From_Date);
} catch (ParseException e) {
e.printStackTrace();
}
Date date2 = null;
try {
date2 = formatter.parse(To_Date);
} catch (ParseException e) {
e.printStackTrace();
}
assert date1 != null;
boolean bool1 = date1.after(date2);
boolean bool2 = date1.before(date2);
boolean bool3 = date1.equals(date2);
if(bool1){
System.out.println(From_Date+" is after "+To_Date);
Toast.makeText(apply.this, "To date cannot be less then From Date", Toast.LENGTH_SHORT).show();
}else if(bool2){
System.out.println(From_Date+" is before "+To_Date);
}else if(bool3){
System.out.println(From_Date+" is equals to "+To_Date);
}
}
};


Post a Comment

0Comments

Post a Comment (0)