0

I want to change the background drawable of a specific table row based on their status compared to their updated date/time with the current time and check if it has been 24h since last update date/time and set a different background drawable.

What I got so far works but I can't figure out how to check if its been 24h and not just greater then the current time

   JobRepository jobRepository = new JobRepository(a.getApplication());
        jobRepository.findEnquiry().observe(getViewLifecycleOwner(), jobsEnquiry -> {
        TableRow[] tableRows = new TableRow[jobsEnquiry.size()];

        for (int i=0; i<jobsEnquiry.size(); i++) {
            Job job = jobsEnquiry.get(i).job;
            tableRows[i] = new TableRow(a);
            tableRows[i].setId(i + 1);
            tableRows[i].setPadding(0,20,0,20);
            if(job.getStatus().equals("Left message/Awaiting reply")){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H:m:s");
                Date updatedDate = job.getUpdatedAt();
                Date date = job.getDate();
                Date currentTime = Calendar.getInstance().getTime();
                tableRows[i].setBackgroundResource(android.R.drawable.list_selector_background);
                tableRows[i].setBackgroundResource(R.drawable.table_outline);

                if (updatedDate.getTime() < currentTime.getTime()){
                    tableRows[i].setBackgroundResource(android.R.drawable.list_selector_background);
                    tableRows[i].setBackgroundResource(R.drawable.table_outline_status);
                }

            }
            else if(job.getStatus().equals("Action required")){
                tableRows[i].setBackgroundResource(android.R.drawable.list_selector_background);
                tableRows[i].setBackgroundResource(R.drawable.table_outline_status);
            }
KCYrgn
  • 251
  • 1
  • 12
  • 1
    Can't you use `java.time` for a task like this? Why use the outdated `java.util.Date`? – deHaar Jun 22 '21 at 09:17
  • This is what works for now and the ```job.getUpdatedAt()``` requires a date plus I am not familiar with ```java.time``` – KCYrgn Jun 22 '21 at 09:29
  • OK, I see... Refactoring huge loads of code is nothing one would prefer, but `java.time` is a real improvement. Will be worth having a look at... – deHaar Jun 22 '21 at 09:35
  • 2
    Additionally that's quite bad from a performance standpoint. Creating a SimpleDateFormat on each iteration is ... expensive. Lots of mixed responsibilities, not to mention you're basically ignoring timezones, and offsets, so a change of time (like daylight saving) can yield incorrect results in this code. – Martin Marconcini Jun 22 '21 at 11:17

2 Answers2

0

The returning value from getTime() on a Date class is a long see: https://developer.android.com/reference/java/util/Date#getTime()

So the below code would work, but it wouldn't handle timezones very well.

private final long TWENTYFOUR_HOURS = TimeUnit.HOURS.toMillis(24);

if ((updatedDate.getTime() + TWENTYFOUR_HOURS) < currentTime.getTime()){
//Do something
}

or

if ((updatedDate.getTime() + TimeUnit.HOURS.toMillis(24)) < currentTime.getTime()){
//Do something
}
Scott Johnson
  • 707
  • 5
  • 13
0

java.time through desugaring

Consider using java.time, the modern Java date and time API, for your date and time work. Also when you get olkd-fashioned Date objects from a legacy API. Convert each Date to a modern Instant first thing.

    Instant twentyfourHoursAgo = Instant.now().minus(24, ChronoUnit.HOURS);
    List<Date> dates = Arrays.asList(new Date(1_624_298_359_544L), new Date(1_624_240_459_544L),
            new Date(1_624_377_259_544L), new Date(1_624_297_759_544L));
    for (Date updatedDate : dates) {
        Instant updatedInstant = DateTimeUtils.toInstant(updatedDate);
        if (updatedInstant.isAfter(twentyfourHoursAgo)) {
            System.out.println("" + updatedDate + ": updated within 24 hours");
        } else {
            System.out.println("" + updatedDate + ": it has been 24 hours since last update");
        }
    }

Output when I ran the code in Europe/Copenhagen time zone a minute ago:

Mon Jun 21 19:59:19 CEST 2021: updated within 24 hours
Mon Jun 21 03:54:19 CEST 2021: it has been 24 hours since last update
Tue Jun 22 17:54:19 CEST 2021: updated within 24 hours
Mon Jun 21 19:49:19 CEST 2021: it has been 24 hours since last update

I have coded for ThreeTen Backport (see below). It offers DateTimeUtils for converting between old-fashioned and modern types. If using either desugaring or native java.time the conversion from Date to Instant is a little bit simpler, just updatedDate.toInstant() (the toInstant method was added to Date in Java 8).

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161