Here you can find the source of getDiffDate(String srcDate, String format, int diff)
public static final String getDiffDate(String srcDate, String format, int diff)
//package com.java2s; //License from project: Apache License import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static final String dtSimple = "yyyy-MM-dd"; public static final String getDiffDate(Date dt, int idiff) { Calendar c = Calendar.getInstance(); c.setTime(dt);// w w w. j ava 2s.c om c.add(Calendar.DATE, idiff); return dtSimpleFormat(c.getTime()); } public static final String getDiffDate(int diff) { Calendar c = Calendar.getInstance(); c.setTime(new Date()); c.add(Calendar.DATE, diff); return dtSimpleFormat(c.getTime()); } public static final String getDiffDate(String srcDate, String format, int diff) { DateFormat f = new SimpleDateFormat(format); try { Date source = f.parse(srcDate); Calendar c = Calendar.getInstance(); c.setTime(source); c.add(Calendar.DATE, diff); return f.format(c.getTime()); } catch (Exception e) { return srcDate; } } /** * yyyy-MM-dd * * @param date * * @return */ public static final String dtSimpleFormat(Date date) { if (date == null) { return ""; } return getFormat(dtSimple).format(date); } private static final DateFormat getFormat(String format) { return new SimpleDateFormat(format); } }