javadatewebsphere-process-server

Easiest way to add a month to a java DATE type without using Calendar or Joda?


I am working in an integrated environment (IBM Process Server) and I am not able to import anything, can only use standard java functionality.

How can I add x number of months to a given date?


Solution

  • Given that you have Date imported by default, you can add a number of months to the date object by the following:

    public void addMonths(Date date, int numMonths){
        date.setMonth((date.getMonth() - 1 + numMonths) % 12 + 1);
    }
    

    NOTE

    You can use external classes from Java SE by using their full package name. i.e., even if you cannot add import java.util.Calendar; to the top of you .java file, you can still create a calendar object by executing java.util.Calendar cal = java.util.Calendar.getInstance();