Java Date Difference getDiffDate(String srcDate, String format, int diff)

Here you can find the source of getDiffDate(String srcDate, String format, int diff)

Description

get Diff Date

License

Apache License

Declaration

public static final String getDiffDate(String srcDate, String format, int diff) 

Method Source Code


//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);
    }
}

Related

  1. getDateDifference(String start, String end)
  2. getDateDifference(String startDateString, String endDateString)
  3. getDateDiffHour(String begindate, String enddate)
  4. getDayDiff(Date firstDate, Date secondDate)
  5. getDiff(Date from, Date to)
  6. getDiffDays(Date from, Date to)
  7. getDiffDays2(Date one, Date two)
  8. getDifference(Date a, Date b)
  9. getDifference(String dateStr1, String dateStr2, char choice)