0

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 want to display only current date people.
I want to display current date people in RecyclerView.
I have add static data in database.
I have disaply in another arralist but could not disaply
I'm new in android programming

My static Database

dd            MMMM
 2          October
14          November
24          October

My Code

 private List<People> notificationList = new ArrayList<>();
private List<People> peopleListSelected = new ArrayList<>();
private RecyclerView recyclerView;
private NotificationAdapter notificationAdapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notificatoin);
    BuildData();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    calender();

    Calendar calendar = Calendar.getInstance();
    for (People people : notificationList) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMM");

            String today = sdf.format(calendar.getTime());
            String startDate = people.getDate() + "-" + people.getMonth();

            if (today.equals(startDate)) {

                Date date = sdf.parse(startDate);

                calendar.setTime(date);
                calendar.set(Calendar.HOUR_OF_DAY, 18);
                calendar.set(Calendar.MINUTE, 35);
                calendar.set(Calendar.SECOND, 0);
                peopleListSelected.add(people);

            }

        } catch (ParseException e) {
            e.printStackTrace();

        }
    }


    Intent intent1 = new Intent(Notification.this, MyReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getService(Notification.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) Notification.this.getSystemService(Notification.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    notificationAdapter = new NotificationAdapter(getApplicationContext(), peopleListSelected);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new

            DefaultItemAnimator()

    );
    recyclerView.setAdapter(notificationAdapter);
    Button backwindow = (Button) findViewById(R.id.backwindow);
    backwindow.setOnClickListener(new View.OnClickListener()

                                  {
                                      public void onClick(View v) {
                                          onBackPressed();
                                      }
                                  }

    );


}

public void calender() {
}

public void clickMe(View view) {
    Intent intent = new Intent(this, NotificationOpen.class);
    this.startActivity(intent);
}

public void onBackPressed() {
    super.onBackPressed();
}

private List<People> BuildData() {
    DataBaseHelper db = new DataBaseHelper(getApplicationContext());

    try {
        db.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }

    if (db.open()) {
        notificationList = db.getPeopleDate();
    }

    return notificationList;

}

}

Joy
  • 289
  • 1
  • 15

1 Answers1

0

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.

MonthDay md = 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.

String output = 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.

ZoneId z = ZoneId.of( "America/Montreal" ); 
LocalDate today = 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.

ZoneId z = 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?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

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.

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