Question

Add one month to date, but keep exact same day or last day of the month if higher

Hello.
I have the following requirement: for a given DateTime, I need to get the same date + one month. However, if the day is, for example, the 31st, and the following month has only 30 days or less, I need it be the last day of the following month.

I could have use a function to add days and add 30 days to the current date, or use an existing function to add one month, but I'm affraid the date could shift on the next month (like changing from 26 to 25 of the month).

In practice, these are some example of results I'd like to have:
26/06->26/07->26/08...
01/06->01/07->01/08...
31/05->30/06->31/07...
30/06->30/07->30/08...

Is there an ootb existing function for this? Or do I need to use java code or activity for this?

Thanks in advance.

**Moderation Team has archived post**

This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.

Correct Answer
June 27, 2017 - 7:28am

I ended up developping my own java function.

It uses only the Calendar library and a String parameter, "theDate". So I guess it could help people having the same requirement.

 

  1. Calendar cal = Calendar.getInstance();
  2. DateTimeUtils dtu = ThreadContainer.get().getDateTimeUtils();
  3.  
  4. //get info of date in parameter
  5. cal.setTime(dtu.parseDateTimeString(theDate));
  6. int year = cal.get(Calendar.YEAR);
  7. int month = cal.get(Calendar.MONTH);
  8. int day = cal.get(Calendar.DAY_OF_MONTH);
  9.  
  10. //get number of max days of next month
  11. Calendar NewCal = Calendar.getInstance();
  12. NewCal.set(year, month, 1);
  13. NewCal.add(Calendar.MONTH, 1);
  14. int nextMax=NewCal.getActualMaximum(Calendar.DAY_OF_MONTH);
  15.  
  16. if(nextMax>=day) //if max day of next month is lower than current day, just add one month
  17. {
  18. cal.set(year,month,1);
  19. cal.add(Calendar.MONTH, 1);
  20. cal.set(Calendar.DAY_OF_MONTH, day);
  21. } else { //if not: go to next month, set day to maximum number of day
  22. cal.set(year,month,1);
  23. cal.add(Calendar.MONTH, 1);
  24. cal.set(Calendar.DAY_OF_MONTH, nextMax);
  25. }
  26.  
  27. return dtu.formatDateTimeStamp(cal.getTime());

 

Comments

Keep up to date on this post and subscribe to comments

Pega
June 26, 2017 - 11:47am

There is no OOTB function for this exact requirement. You need to write your own logic for this.

 

Let us know if you need any information :)

June 27, 2017 - 7:28am

I ended up developping my own java function.

It uses only the Calendar library and a String parameter, "theDate". So I guess it could help people having the same requirement.

 

  1. Calendar cal = Calendar.getInstance();
  2. DateTimeUtils dtu = ThreadContainer.get().getDateTimeUtils();
  3.  
  4. //get info of date in parameter
  5. cal.setTime(dtu.parseDateTimeString(theDate));
  6. int year = cal.get(Calendar.YEAR);
  7. int month = cal.get(Calendar.MONTH);
  8. int day = cal.get(Calendar.DAY_OF_MONTH);
  9.  
  10. //get number of max days of next month
  11. Calendar NewCal = Calendar.getInstance();
  12. NewCal.set(year, month, 1);
  13. NewCal.add(Calendar.MONTH, 1);
  14. int nextMax=NewCal.getActualMaximum(Calendar.DAY_OF_MONTH);
  15.  
  16. if(nextMax>=day) //if max day of next month is lower than current day, just add one month
  17. {
  18. cal.set(year,month,1);
  19. cal.add(Calendar.MONTH, 1);
  20. cal.set(Calendar.DAY_OF_MONTH, day);
  21. } else { //if not: go to next month, set day to maximum number of day
  22. cal.set(year,month,1);
  23. cal.add(Calendar.MONTH, 1);
  24. cal.set(Calendar.DAY_OF_MONTH, nextMax);
  25. }
  26.  
  27. return dtu.formatDateTimeStamp(cal.getTime());