Java Day Add addDate(Date date, int day)

Here you can find the source of addDate(Date date, int day)

Description

add Date

License

Apache License

Declaration

public static Date addDate(Date date, int day) 

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 Date addDate(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        long millis = getMillis(date) + day * 24L * 3600L * 1000L;
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    }//from  w  ww  . j ava2s .c om

    public static long getMillis(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }

    public static String getTime(Date date) {
        return format(date, "HH:mm:ss");
    }

    public static String format(Date date, String format) {
        String result = "";
        try {
            if (date != null) {
                DateFormat dateFormat = new SimpleDateFormat(format);
                result = dateFormat.format(date);
            }
        } catch (Exception localException) {
        }
        return result;
    }

    public static String format(Date date) {
        return format(date, "yyyy-MM-dd");
    }
}

Related

  1. add(int date, int days)
  2. add(String date, int day)
  3. addDate(Date d, long day)
  4. addDate(java.util.Date date, int day)
  5. addDate(String date, SimpleDateFormat format, int dayCnt)
  6. addDate(String str, int field, int dayNum)
  7. addDay(Date d, int amount)