Best Way to Compare Dates in Android

Best way to compare dates in Android

Your code could be reduced to

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date strDate = sdf.parse(valid_until);
if (new Date().after(strDate)) {
catalog_outdated = 1;
}

or

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date strDate = sdf.parse(valid_until);
if (System.currentTimeMillis() > strDate.getTime()) {
catalog_outdated = 1;
}

How to compare Two date in android?

You can parse the yyyy-mm-dd HH:mm:ss date thanks to DateFormat class (e.g. the SimpleDateFormat class). This operation will return a Date object.

You can also create a Date object from current time in milliseconds.

When you have both objects you can compare them thanks to compareTo method.

For instance the following code:

try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsed = sdf.parse("2016-03-10 22:05:20");

Date now = new Date(System.currentTimeMillis()); // 2016-03-10 22:06:10

System.out.println(parsed.compareTo(now));
} catch (Exception e) {
e.printStackTrace();
}

will print -1, which means that parsed is before now.

EDIT:

Here it is the code of a simple but useless application that makes use of AlarmManager.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Log.d("TAG", "From intent: "+getIntent().getStringExtra("MyEXTRA"));
}

@Override
protected void onResume() {
super.onResume();

try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long parsedMillis = sdf.parse("2016-03-10 22:54:30").getTime();
long now = System.currentTimeMillis(); // 22:54:15

if (parsedMillis > now) {
Log.d("TAG", "In the future!");

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("MyEXTRA", "From alarm");
PendingIntent broadcast = PendingIntent.getActivity(this, 0, intent, 0);
am.setExact(AlarmManager.RTC_WAKEUP, parsedMillis, broadcast);

} else {
Log.d("TAG", "In the past...");
}

} catch (Exception e) {
e.printStackTrace();
}
}

In the LogCat you will see:

03-10 22:54:20.925    3946-3946/com.example.myapp D/TAG﹕ From intent: null
03-10 22:54:21.227 3946-3946/com.example.myapp D/TAG﹕ In the future!
03-10 22:54:30.513 3946-3946/com.example.myapp D/TAG﹕ From intent: From alarm
03-10 22:54:30.577 3946-3946/com.example.myapp D/TAG﹕ In the past...

The last line is printed because the alarm causes the activity to be relaunched, hence the onResume will be called another time and System.currentTimeMillis() will be greater than the hardcoded time. As you can see, however, the alarm is correctly fired at the scheduled time.

Moreover you should consider edge cases: if parsedMillis is greater than now by only a few milliseconds, you could set an alarm that will never be fired because it is set in the past. This happens because the OS needs some time to execute your code, so you should check that parsedMillis > now + DELTA, where DELTA is a reasonable amout of time that depends on the code you execute between System.currentTimeMillis() and am.setExact(...).

How to compare if a date is greater than other?

To compare dates you can convert to Date and use:

final Date other = simpleDateFormat.parse("2022-03-07");
final Date now = new Date();

if (other.after(now)) {
// In the future!
} else if(other.before(now)) {
// In the past!
} else {
// In the present!
}

How to compare two dates in Android

For the first item in RecyclerView, position will be 0 which makes position - 1 = -1 which is an invalid index hence the exception, in onBindViewHolder check for position > 0 before doing -1

@Override
public void onBindViewHolder(@NonNull @NotNull myadapter.ViewHolder holder, int position) {
Chat name=values.get(position);
holder.txtmsg.setText(name.getMessage());
if(position > 0)
Chat pname=values.get(position - 1);
if(!name.getDate().equals(pname.getDate())) {
holder.time.setText(name.getTime());
}else{
holder.date.setVisibility(View.INVISIBLE);
}
}
if(position==values.size()-1){
if(name.getIsseen()){
holder.textseen.setText("SEEN");
}else{
holder.textseen.setText("DELIVERED");
}
}
}

Android - Compare two dates

If you just want to compare the Strings it would look like this:

    Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

String tomorrowAsString = dateFormat.format(tomorrow);

calendar = Calendar.getInstance();
Date today = calendar.getTime();

String todayAsString = dateFormat.format(today);

if(tomorrowAsString.equals(todayAsString))
System.out.println(true);
else
System.out.println(false);


Related Topics



Leave a reply



Submit