Nancy
Nancy

Reputation: 1051

Take current year and set it to calendar

I'm trying to make this code dynamically work so that I don't have to manually change and update the code for it to work when the new year comes around.

The way my calendar works is that I need to set a start date and end date which goes from the current January to the next January, this is manual right now.

This is where I set it for the calendar, and I also use these dates to set a RecyclerView

widget.state().edit()
              .setMinimumDate(CalendarDay.from(2020, 1, 1))
              .setMaximumDate(CalendarDay.from(2021, 1, 31))
              .commit();
String firstDate = "2020-01-01 00:00:00";
String endDate = "2021-01-31 00:00:00";

List<Events> listado = SQLite.select().from(Events.class)
        .where(Events_Table.end_date.greaterThanOrEq(firstDate))
        .and(Events_Table.init_date.lessThanOrEq(endDate))
        .orderBy(Events_Table.init_date.asc())
        .queryList();

How can I do this dynamically?

Upvotes: 1

Views: 377

Answers (1)

Joakim Danielson
Joakim Danielson

Reputation: 51993

Not sure what type the set methods expects but here is a simple way to get this and next January 1st

Year thisYear = Year.now();
LocalDate thisJanuaryFirst = thisYear.atDay(1);
LocalDate nextJanuaryFirst = thisYear.plusYears(1).atDay(31);

Of course if you want to make it really simple you could create strings directly

String firstDate = String.format("%d-01-01", thisYear.getValue());
String secondDate = String.format("%d-01-31", thisYear.plusYears(1).getValue());

Upvotes: 4

Related Questions