cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the last day of the previous month

former_member190358
Participant
0 Kudos

Hello Team,

If  my input date is today , then i need to find out the last day of the previous month for the same.

Can someone help me  to find out .. how can this be done.

Regards,

Ravi

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi Ravi,

I have try with this UDF.

Calendar cal = Calendar.getInstance();

cal.set(Calendar.DATE,1);

cal.add (Calendar.DAY_OF_MONTH,-1);

Date lastDateOfPreviousMonth=cal.getTime();

String t=lastDateOfPreviousMonth+"";

return t.substring(24,28)+t.substring(4,7)+t.substring(8,10);

Try in implementing the above UDF and update the results.

Thanks,

Durga

Former Member
0 Kudos

Hi,

Try the below code.

     // get a calendar object
    GregorianCalendar calendar = new GregorianCalendar();
   
    // convert the year and month to integers
    int yearInt = Integer.parseInt(year);
    int monthInt = Integer.parseInt(month);

     int dayInt = Integer.parseInt(day);
   
    // adjust the month for a zero based index
    monthInt = monthInt - 1;
   
    // set the date of the calendar to the date provided
    calendar.set(yearInt, monthInt, dayInt);
   
    int day = calendar.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
   
    return Integer.toString(day);

Regards

Venkat

Harish
Active Contributor
0 Kudos

Hi Ravi,

If you want in Java then use the calender function

Calendar aCalendar = Calendar.getInstance();

// add -1 month to current month

aCalendar.add(Calendar.MONTH, -1);

// set DATE to 1, so first date of previous month

aleaCalendar.setCalendar.DATE, 1);

Date firstDateOfPreviousMonth = aCalendar.getTime();

// set actual maximum date of previous month

aleaCalendar.setCalendar.DATE,     aleaCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));

//read it

Date lastDateOfPreviousMonth = aCalendar.getTime();

regards,

Harish

Former Member
0 Kudos

Hi Ravi,

Please try with 'OIL_LAST_DAY_OF_PREVIOUS_MONTH' FM

Give today's day as input you will get last month end date.

former_member190358
Participant
0 Kudos

Hi Payel,

I want to get this function in SAP PI ... Can i use it in UDF .

Ravi