Saturday, May 14, 2011

Subtract ndays from given Date

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
   /**
         * @param date
         * @param days
         * @param format
         * @return
         */
        public static String subtractDays(Date date, Integer days,String format) {

              
                String formattedDate = null;
                if(null != date && null != format){
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(date);
                        calendar.add(Calendar.DATE, -days);
                        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
                        formattedDate=dateFormat.format(calendar.getTime());
                }
                return formattedDate;
        }

No comments:

Post a Comment