Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

last day of month calculation

I am having issues with the calculation of when the next Last Day of the Month is for a notification which is scheduled to be sent.

Here is my code:

RecurrenceFrequency recurrenceFrequency = notification.getRecurrenceFrequency();
Calendar nextNotifTime = Calendar.getInstance();

This is the line causing issues I believe:

nextNotifTime.add(recurrenceFrequency.getRecurrencePeriod(), 
                  recurrenceFrequency.getRecurrenceOffset());

How can I use the Calendar to properly set the last day of the next month for the notification?

like image 843
OakvilleWork Avatar asked Oct 10 '22 02:10

OakvilleWork


People also ask

How do I calculate the last date of a month in Excel?

=EOMONTH(A2, 1) - returns the last day of the month, one month after the date in cell A2. =EOMONTH(A2, -1) - returns the last day of the month, one month before the date in cell A2. Instead of a cell reference, you can hardcode a date in your EOMONTH formula.

How do you calculate EOM in Excel?

The method to use this function is as follows =EOMONTH( start_date, months). Here, to add the last day of December (4+8), the Excel EOMONTH function added 8 and returned the result as 31/12/2020.

How to calculate the last date of the month in Excel?

The “Date” should be in the “MM_DD_YY” format. If the “Date” is not an incorrect format, there will be a #NUM error. We can calculate the day that falls on the last date of the month by using the “Text” function of Excel.

How to get the last day of the previous month?

When you supply zero as the day argument to DATE, the date function will "roll back" one day to the last day of the previous month. So, by adding 1 to the month, and using zero for day, DATE returns the last day of the "original" month. Although EOMONTH is a more convenient function, it won't accept a range...

How to compute distance in months and days between two dates?

To compute the distance in months and days between two dates, simply fill out the two input fields: First date: Enter the date to start the calculation Second date: Enter the end date for the calculation

How to find end of month and end of Quarter dates?

How to find end of month date or end of quarter date. Environment Tableau Desktop Answer. See the attached workbook. To find and use end of month dates: Create a calculated field with the following calculation: DAY(DATEADD('day', 1, [Order Date])) = 1; Add calculated field to the filters shelf. Select True. To find and use end of quarter dates:


2 Answers

Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);

This returns actual maximum for current month. For example it is February of leap year now, so it returns 29 as int.

like image 379
AlexR Avatar answered Oct 11 '22 16:10

AlexR


java.time.temporal.TemporalAdjusters.lastDayOfMonth()

Using the java.time library built into Java 8, you can use the TemporalAdjuster interface. We find an implementation ready for use in the TemporalAdjusters utility class: lastDayOfMonth.

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

LocalDate now = LocalDate.now(); //2015-11-23
LocalDate lastDay = now.with(TemporalAdjusters.lastDayOfMonth()); //2015-11-30

If you need to add time information, you may use any available LocalDate to LocalDateTime conversion like

lastDay.atStartOfDay(); //2015-11-30T00:00
like image 72
Przemek Avatar answered Oct 11 '22 14:10

Przemek