3

I am getting the current date like below:

public void getCurrentDate(View view) {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat mdformat = new SimpleDateFormat("yyyy / MM / dd ");
    String strDate = "Current Date : " + mdformat.format(calendar.getTime());
    display(strDate);
}
private void display(String num) {
    TextView textView = (TextView) findViewById(R.id.current_date_view);
    textView.setText(num);
}

But I want to check whether the date is today's date or not. But I am not getting how to check that.

paulXkt
  • 93
  • 4
Ravi kumar
  • 41
  • 1
  • 2
  • 9
  • 2
    Unfortunately, Android will give you the system date which can be easily changed from settings. To have actual date you need to verify using internet – Deepak kaku Apr 09 '18 at 18:47
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Apr 10 '18 at 17:23

2 Answers2

4

You can do as @aliaksei's answer, which works fine. But in Android you can also use java.time (API level 26) or ThreetenABP if java.time is not available.

But first you must consider the fact that "today's date" is relative. Now, at this very moment, each part of the world might be in a different "today". In America and Europe it's April 10th, but in some parts of the Pacific it's already April 11th.

You can choose a specific timezone, or just use the device's zone, which I think it's the case. In this case, just use the JVM default timezone.

Then you use the ThreetenABP to convert the Calendar to a LocalDate and then compare with today's date:

// calendar I want to check
Calendar cal = ...

// convert to LocalDate
LocalDate date = DateTimeUtils.toZonedDateTime(cal).toLocalDate();

// compare with today (now() is using the JVM default timezone)
if (date.isEqual(LocalDate.now(ZoneId.systemDefault()))) {
    // today
}
paulXkt
  • 93
  • 4
  • @OleV.V. To me it's still confusing that there are 2 methods: equals and isEqual. Reading the docs I saw that there's really a subtle difference between them. Anyway, thanks! – paulXkt Apr 10 '18 at 16:25
  • The difference becomes clearer when you compare for example `ZonedDateTime` objects. `isEqual` will tell you whether they denote the same point in time, while `equals` will check whether they are exactly equal (same zone and more). Most of all I dislike the type unsafety of `equals`: it accepts any `Object` without objection, potentially leaving you with very strange results. – Ole V.V. Apr 10 '18 at 16:29
1
public static boolean isSameDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        throw new IllegalArgumentException("The dates must not be null");
    }
    return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
            cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
            cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}

Or you could use the DateUtils class that this snippet is from :)

aliaksei
  • 714
  • 1
  • 9
  • 23