-1

2019-12-07 20:13:04

i need if this date is in today my buton visibility is visible.

try {
        String dtStart = sales.getDate();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        Date date = format.parse(dtStart);


        Calendar now = Calendar.getInstance();
        Date today = now.getTime();
        if (date == today){
            holder.delete.setVisibility(View.VISIBLE);
        }else {
            holder.delete.setVisibility(View.INVISIBLE);
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mert Malaves
  • 43
  • 1
  • 7

4 Answers4

4

Use the java-8 date time API, first your formatter is wrong month should represent with upper case letters MM, and use DateTimeFormatter instead of legacy SimpleDateFormat

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

And then parse the input string into LocalDateTime using formatter

String date = "2019-12-07 20:13:04";

LocalDateTime dateTime = LocalDateTime.parse(date,formatter);

Finally compare the input date with current date using equals, here is the information to get java-8 date time API on android

dateTime.toLocalDate().equals(LocalDate.now());  //true
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
2

You can use DateUtils.isToday method, to check if date is today:

try {
    String dtStart = sales.getDate();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
    Date date = format.parse(dtStart);

    boolean isToday = DateUtils.isToday(date.getTime());
    if (isToday){
        holder.delete.setVisibility(View.VISIBLE);
    }else {
        holder.delete.setVisibility(View.INVISIBLE);
    }
} catch (ParseException e) {
    e.printStackTrace();
}
Akaki Kapanadze
  • 2,552
  • 2
  • 11
  • 21
1

If you already converted your date string to a date you can convert the Date-Object to a Calendar-Object and compare the year, month and day.

Example implementation:

private boolean isToday(Date date) {
        Calendar calendar = Calendar.getInstance();
        Calendar toCompare = Calendar.getInstance();
        toCompare.setTimeInMillis(date.getTime());

        return calendar.get(Calendar.YEAR) == toCompare.get(Calendar.YEAR)
            && calendar.get(Calendar.MONTH) == toCompare.get(Calendar.MONTH)
            && calendar.get(Calendar.DAY_OF_MONTH) == toCompare.get(Calendar.DAY_OF_MONTH);
    }

Alternatively you can convert Date and Calendar to milliseconds and compare with the todays milliseconds.

Example implementation:

private boolean isToday2(Date dateToCheck) {
        Calendar calendar = Calendar.getInstance();

        // you have to set calendar object to 00:00:00
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        // milliseconds of 1 day = 86400000
        return dateToCheck.getTime() - calendar.getTimeInMillis() < 86400000; 
    }

Both solutions do not handle however localized time correctly. So use with caution.

malliaridis
  • 389
  • 1
  • 12
0

We can use Java 8 Date-Time api as below :

    LocalDate dt2= LocalDate.of(2019,10,20);

    String dtStart= dt2.format(DateTimeFormatter.ISO_DATE);

    LocalDate currentDt= LocalDate.now();

    if(currentDt.format(DateTimeFormatter.ISO_DATE).equals(dtStart)) {
        // logic here 
    }else {
        //logic here
    }
  • 1
    Using `LocalDate` is a good idea. Formatting before comparing is over-complicating things. Just comapre the two `LocalDate` objects as shown in [the answer by Deadpool](https://stackoverflow.com/a/59229116/5772882). – Ole V.V. Dec 10 '19 at 21:11