Java Utililty Methods Date Calculate

List of utility methods to do Date Calculate

Description

The list of methods to do Date Calculate are organized into topic(s).

Method

DatecalculateTime(Date date, String addpart, int num)
calculate Time
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
if (addpart.equalsIgnoreCase("Y")) {
    cal.add(Calendar.YEAR, num);
} else if (addpart.equalsIgnoreCase("M")) {
    cal.add(Calendar.MONTH, num);
} else if (addpart.equalsIgnoreCase("D")) {
    cal.add(Calendar.DATE, num);
...
StringcalculateTime(Date sendTime, String timeZoneID)
calculate Time
if (sendTime == null) {
    return "NOW";
} else {
    long date = sendTime.getTime();
    TimeZone tz = TimeZone.getTimeZone(timeZoneID);
    date -= tz.getRawOffset();
    if (tz.inDaylightTime(sendTime)) {
        date -= 3600 * 1000;
...
StringcalculateTimestamp()
This method is used to calculate the current timestamp of a request.
Calendar cal = new GregorianCalendar();
sdf.setTimeZone(cal.getTimeZone());
return sdf.format(cal.getTime());
intcalculateTruncatedTime(final Date dateTimeBegin, final Date dateTimeEnd, final Date begin, final Date end)
Calculate quantity of minutes of task.
if (begin.compareTo(end) > 0) {
    final Calendar calendarBegin1 = new GregorianCalendar();
    calendarBegin1.setTime(begin);
    calendarBegin1.set(Calendar.HOUR_OF_DAY, 0);
    calendarBegin1.set(Calendar.MINUTE, 0);
    calendarBegin1.set(Calendar.SECOND, 0);
    final Calendar calendarEnd2 = new GregorianCalendar();
    calendarEnd2.setTime(calendarBegin1.getTime());
...
intcalculateTruncatedTimeByDays(final Date dateTimeStart, final Date dateTimeEnd, final Date start, final Date end)
Retrieves time of activity on period.
int minutes = 0;
final Date startActivity = new Date(dateTimeStart.getTime());
final Date endActivity = new Date(dateTimeEnd.getTime());
final Date periodBegin = new Date(start.getTime());
final Date periodEnd = new Date(end.getTime());
if (startActivity.compareTo(endActivity) < 0) {
    final Calendar calendarBegin = new GregorianCalendar();
    calendarBegin.setTime(periodBegin);
...
intcalculationAge(Date birthday, Date calculateDay)
calculation Age
@SuppressWarnings("unused")
int day = 1, month = 0, year = 1, ageYears, ageMonths, ageDays;
Calendar calculate = Calendar.getInstance();
calculate.setTime(calculateDay);
Calendar birth = Calendar.getInstance();
birth.setTime(birthday);
try {
    year = birth.get(Calendar.YEAR);
...
DategetDateCalculate(int d)
get Date Calculate
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, d);
return calendar.getTime();