1

For my app I need a function that only shows the to-dos which due date is today. I am new to Android Studio and Java, so unfortunately I do not know how to write this code.

This is my code so far:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getIdemid()) {
        case R.id.menu_dailyToDo:
            showDailyTodo();
            adapter.notifyDataSetChanged();
            return true;
       }
    }
}

public void showDailyTodo() {
    if(tvDueDate == //today) {
        // code that listview items with duedate today are shown.
    }
}

All I could find so far could not help me.

Edit

In the picture you can see my listview.

enter image description here

The date is a TextView with ID tvDueDate.

halfer
  • 19,824
  • 17
  • 99
  • 186
diem_L
  • 389
  • 5
  • 22
  • 1
    Remove `Android-Studio` tag, this causes a confusion that you are talking about `ToDo`s inbuilt in `Android-Studio`. Add `android` tag instead – letsintegreat Jan 12 '19 at 06:31

3 Answers3

2

You can use System.currentTimeMillis() to get current time upto milliseconds as long datatype.

OR

You can use Date d = new Date(); to get the date as a Date object and convert it to String in desirable format like

Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(d);

It depends on datatype and format in which you have stored dates in tvDueDate.

Then you can set Todos Items into a ListView adapter as shown in this post Custom Adapter for List View

EDIT

you can get your due date in a String using

String dueDate = tvDueDate.getText().toString();

Then do

public void showDailyTodo() {
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd.mm.yyyy");
String formattedDate = df.format(d);
if(dueDate.equals(formattedDate)) {
    // code that listview items with duedate today are shown.
}
}
Issac Peter
  • 144
  • 10
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android (<26) in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Jan 12 '19 at 18:46
2

Avoid legacy date-time classes

You are using terrible old date-time classes that were supplanted years ago by the modern java.time classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.

Your ToDo class should have a member variable of type LocalDate. And optionally a getter method.

public class ToDo {
    public LocalDate whenDue ;

}

Time zone

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the code becomes ambiguous to read in that we do not know for certain if you intended to use the default or if you, like so many programmers, were unaware of the issue.

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.

LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ;  // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.

Or, better, use the Month enum objects pre-defined, one for each month of the year. Tip: Use these Month objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety. Ditto for Year & YearMonth.

LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;

Compare

Compare using methods on LocalDate: isEqual, isBefore, isAfter, equals.

Loop your to-do items, comparing the date to the target, today's date.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;  // Or `ZoneId.systemDefault()`.
LocalDate ld = LocalDate.now( z ) ;
List< ToDo > todayToDos = new ArrayList<>() ;
for( ToDo todo : todos ) {
    if( todo.whenDue.isEqual( today ) ) {
        todayToDos.add( todo ) ;
    }
}

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

You can use the DateUtils library in android to check if the due date is today or not.

Here is a link to the official documentation: https://developer.android.com/reference/android/text/format/DateUtils#isToday(long)

For getting listview items, just loop through the items and check if their due date is today or not and filter out the items. You can pass those items to the adapter of the listview. For comparison of individual item, a rough implementation would be:

long timeInMillis = dueDate.getTime();
if (dateUtils.isToday(timeInMillis)){
    //this is today's item, so add this to a list which is sent to the listview
}
sk_462
  • 587
  • 1
  • 7
  • 16
  • this sounds exactly what i need, but I am not able to write the correct code :( – diem_L Jan 12 '19 at 07:10
  • Which specific part are you having trouble with? If you have a class for todoItem which consists of due date and an arraylist, it should be easy to filter out your required result. – sk_462 Jan 12 '19 at 07:16
  • the big problem is, i do not know how to compare the due date from the todo with the current date. I added a screenshot of my listview with four todos. – diem_L Jan 12 '19 at 07:22
  • If your dueDate is a date object, I have added some code for comparison above which should work. – sk_462 Jan 12 '19 at 07:28