Question

How to calculate Notice Period date?

I want to calculate Notice period date from the date person has resigned.? Notice period has to be 1 month and the current month employee has resigned.

For ex-

If a person resigned on 6th Dec then Notice period has to be 31st Jan.

***Moderator Edit-Vidyaranjan: Updated Platform Capability***

Correct Answer
December 6, 2019 - 3:10am

You can write a function something like this : GetLastDayOfNextMonth()

Parameter : theDateStr

 

java.util.Calendar theDate;
java.util.GregorianCalendar futureDate;
Date parseDate;

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");

if (theDateStr.equals(""))  {
    // If date not supplied, default to Now.
    String sTimeZone = ThreadContainer.get().getRequestorPage().getIfPresent("pyUseTimeZone").getStringValue();
    theDate= new java.util.GregorianCalendar(TimeZone.getTimeZone(sTimeZone));
}  else  {
    try {
        parseDate = sdf.parse(theDateStr);
    }
    catch (Exception e) {  
        throw new InvalidParameterException("theDateStr", "RUF-addToDateYM", "getFirstDayOfNextMonth(String)", "value " + theDateStr + " is not a valid Date or DateTime string");
    }
    theDate = java.util.Calendar.getInstance();
    theDate.setTime(parseDate);
}

// Move to last day of same month
futureDate = new java.util.GregorianCalendar(theDate.get(java.util.Calendar.YEAR),
                                             theDate.get(java.util.Calendar.MONTH),
                                             theDate.getActualMaximum(theDate.DAY_OF_MONTH), //finds the last day of the current month
                                             theDate.get(java.util.Calendar.HOUR_OF_DAY),
                                             theDate.get(java.util.Calendar.MINUTE),
                                             theDate.get(java.util.Calendar.SECOND));

// Move to next month
futureDate.add(java.util.Calendar.MONTH, 1);

return sdf.format(futureDate.getTime());

Comments

Keep up to date on this post and subscribe to comments

December 6, 2019 - 3:10am

You can write a function something like this : GetLastDayOfNextMonth()

Parameter : theDateStr

 

java.util.Calendar theDate;
java.util.GregorianCalendar futureDate;
Date parseDate;

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");

if (theDateStr.equals(""))  {
    // If date not supplied, default to Now.
    String sTimeZone = ThreadContainer.get().getRequestorPage().getIfPresent("pyUseTimeZone").getStringValue();
    theDate= new java.util.GregorianCalendar(TimeZone.getTimeZone(sTimeZone));
}  else  {
    try {
        parseDate = sdf.parse(theDateStr);
    }
    catch (Exception e) {  
        throw new InvalidParameterException("theDateStr", "RUF-addToDateYM", "getFirstDayOfNextMonth(String)", "value " + theDateStr + " is not a valid Date or DateTime string");
    }
    theDate = java.util.Calendar.getInstance();
    theDate.setTime(parseDate);
}

// Move to last day of same month
futureDate = new java.util.GregorianCalendar(theDate.get(java.util.Calendar.YEAR),
                                             theDate.get(java.util.Calendar.MONTH),
                                             theDate.getActualMaximum(theDate.DAY_OF_MONTH), //finds the last day of the current month
                                             theDate.get(java.util.Calendar.HOUR_OF_DAY),
                                             theDate.get(java.util.Calendar.MINUTE),
                                             theDate.get(java.util.Calendar.SECOND));

// Move to next month
futureDate.add(java.util.Calendar.MONTH, 1);

return sdf.format(futureDate.getTime());

December 30, 2019 - 3:32am
Response to RRajeev

Thanks Rajeev.

Pega
December 6, 2019 - 2:24pm

Hi Mohan

Have you considered using the OOTB addDays function?

See screenshot.

 

Pega
December 20, 2019 - 3:53pm

If this is for a report you can also do this in a SQL function alias, and have the database calculate the date for you. From your description above, you would need the following built in SQL functions:

MONTH(date) - To get the month the person resigned

DATE_ADD(date, interval) - To get to the following (i.e., the notice period) month

LAST_DAY(date) - to get the last date of the notice period month