Seb
Seb

Reputation: 3215

Find in the last day of the month using calendar on Android

I am trying to change the last day allowed in my calendar Widget based on the current date or fix date.

The code below is using a date as a default start date and I need to allow the user to select between this date and date + 3 months later.

val maxDay = Calendar.getInstance() 

setSelectedDate(currentShipDate.toDate())

maxDay.add(Calendar.MONTH, 3)

state().edit()
     .setMinimumDate(minDay.time)
     .setMaximumDate(maxDay.time)
     .commit()

The maxDay is now set to 3 months later using the currentShipDate. However, I am looking to allow the user to also select a date until the end of the 3rd months.

For example, if the ship happened in 21st July, you can change the shipping date til 31st October (instead of 21st October)

For example, if the ship happened in 10th July, you can change the shipping date til 31st October (instead of 10st October)

Any idea ?

Thanks for your help

Upvotes: 0

Views: 767

Answers (1)

jmart
jmart

Reputation: 2931

Do it like this:

  1. Instead of adding 3 months, add 4 --> maxDay.add(Calendar.MONTH, 4)
  2. Set the day of the month to 1 --> maxDay.set(Calendar.DAY_OF_MONTH, 1)
  3. Subtract one day --> maxDay.add(Calendar.DAY_OF_MONTH, -1)

This will give you the last day of the third month.

Upvotes: 2

Related Questions