Java Time Add addTime(Date date, int field, int value)

Here you can find the source of addTime(Date date, int field, int value)

Description

add Time

License

LGPL

Declaration

public static Date addTime(Date date, int field, int value) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import static java.util.Calendar.*;

public class Main {
    public static Date addTime(Date date, int field, int value) {
        Calendar cal = cal(date);
        cal.add(field, value);/*from w w w  .  j  a  v a 2 s. c  om*/
        return cal.getTime();
    }

    public static Calendar cal(Date date) {
        if (date == null) {
            return null;
        } else {
            return cal(date.getTime());
        }
    }

    public static Calendar cal(Date date, Integer hour, Integer minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(HOUR_OF_DAY, hour);
        calendar.set(MINUTE, minutes);
        calendar.set(SECOND, 0);
        calendar.set(MILLISECOND, 0);
        return calendar;
    }

    public static Calendar cal(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar;
    }

    public static Calendar cal(int year, int month, int date) {
        return new GregorianCalendar(year, month, date);
    }

    public static Calendar cal(int year, int month, int date, int hour, int minutes) {
        return new GregorianCalendar(year, month, date, hour, minutes);
    }
}

Related

  1. addTime(Date aDate, int timeToAdd, int timeUnits)
  2. addTime(Date baseDate, int amount, int datePart)
  3. addTime(Date current, Date toadd)
  4. addTime(Date date, Date addTime)
  5. addTime(Date date, int field, int value)
  6. addTime(Date date, int mode, int amount)
  7. addTime(Date date, String time)
  8. addTime(Date source, int hours, int minutes, int second)
  9. addTimeByDate(int dateNum, Date date)