Skip to content Skip to sidebar Skip to footer

How To Set Date And Month Wise Notification List Using Database In Android

I have Create BDay app in notification class show to all people list but i want to display current date only. In this code i have to compare to current date and my database date i

Solution 1:

This work would be much easier if you use standard formats for your birth dates, and use the java.time classes that supplant the troublesome old legacy date-time classes.

ISO 8601

The ISO 8601 format defines textual formats for date-time values. For a date-only value, the format is YYYY-MM-DD such as 1955-01-23. For a month-day without a year, replace the year with a hyphen: --01-23. That could be stored in your database as text.

MonthDay

Java offers the MonthDay class for representing, well, a month and a day-of-month. Automatically parses and generates the ISO 8601 strings discussed above.

MonthDaymd= MonthDay.parse( "--01-23" );

You can store such MonthDay objects on your classes, such as birthDate on your Person class. Doing so makes your code more self-documenting, provides type-safety, and guarantees valid values (no February 31, for example).

To write this value to the database, generate a String by calling toString.

Stringoutput= md.toString();  // Ex: --01-23

LocalDate

The LocalDate class represents a date-only value without time-of-day and without 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.

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

You could ask for the JVM’s current default time zone. But know that the current default can be changed at any time by any code in any thread of any app sharing this JVM. So when important, ask/confirm the time zone with the user.

ZoneIdz= ZoneId.systemDefault();  // Caution: Can change at any moment.

To see if a particular MonthDay object is a birthday to celebrate today, compare.

MonthDay mdToday = MonthDay.from( today );
…
if( md.equals( mdToday ) ) {
    … // Add to collection.
}

For example.

MonthDay mdToday = MonthDay.from( LocalDate.now( ZoneId.of( "America/Montreal" ) ) );

List<Person> birthdayCelebrants = new ArrayList<>();
for( Person person: people ) {
    if( person.getBirthMonthDay().equals( mdToday ) ) {
        birthdayCelebrants.add( person );
    }
}

To generate strings for presentation of the date value, search Stack Overflow for the DateTimeFormatter class.


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 java.time.

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

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.

Post a Comment for "How To Set Date And Month Wise Notification List Using Database In Android"