Java Year Month addDate(Calendar cal, int year, int month, int day)

Here you can find the source of addDate(Calendar cal, int year, int month, int day)

Description

add Date

License

Open Source License

Declaration

public static Date addDate(Calendar cal, int year, int month, int day) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    public static Date addDate(Calendar cal, int year, int month, int day) {
        int y = cal.get(Calendar.YEAR);
        int m = cal.get(Calendar.MONTH);
        int d = cal.get(Calendar.DATE);

        cal.set(y + year, m + month, d + day);

        return cal.getTime();
    }/*ww  w  .java  2 s  .  c om*/

    public static Date getTime(String hhmm) {
        if (hhmm != null && hhmm.length() == 4 && isDigit(hhmm)) {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hhmm.substring(0, 2)));
            cal.set(Calendar.MINUTE, Integer.parseInt(hhmm.substring(2, 4)));

            return cal.getTime();
        }

        return null;
    }

    private static boolean isDigit(String digitStr) {
        if (digitStr != null) {
            for (int i = 0; i < digitStr.length(); i++)
                if (!Character.isDigit(digitStr.charAt(i)))
                    return false;
        }
        return true;
    }
}

Related

  1. AddMonths(int yearMonth, int interval)
  2. calculatorDate(int incrementYear, int incrementMonth, int incrementDay, int incrementHour, int incrementMin, int incrementSec)
  3. createCalendar(int year, int month, int date, int hour, int minute, int second)
  4. createDate(final int year, final int month, final int day, final int hour, final int minutes, final int seconds, final int miliseconds)