Java Day of Work addWorkdays(Date date, int workdayOffset)

Here you can find the source of addWorkdays(Date date, int workdayOffset)

Description

Die Anzahl Arbeitstage (d.h.

License

Apache License

Parameter

Parameter Description
date Nie <code>null</code>
workdayOffset Offset in Tagen (1=morgen)

Return

Nie null (Tagesdatum ohne

Declaration

public static Date addWorkdays(Date date, int workdayOffset) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**/*from   w  ww .  j av a2 s .c  om*/
     * Die Anzahl Arbeitstage (d.h. nicht Samstag und Sonntag) addieren.
     *
     * @param date Nie <code>null</code>
     * @param workdayOffset Offset in Tagen (1=morgen)
     * @return Nie <code>null</code> (Tagesdatum ohne
     */
    public static Date addWorkdays(Date date, int workdayOffset) {
        final Calendar cal = getCalendarInstance();
        cal.setTime(date);

        ensureWorkday(cal);
        for (int countdown = workdayOffset; countdown > 0; countdown--) {
            cal.add(Calendar.DAY_OF_YEAR, 1);
            ensureWorkday(cal);
        }

        final Date result = cal.getTime();
        return result;
    }

    /**
     * Locale?
     *
     * @return Eine neue Instanz.
     */
    private static Calendar getCalendarInstance() {
        final Calendar cal = Calendar.getInstance();
        return cal;
    }

    /**
     * Ensure workday.
     *
     * @param cal the cal
     */
    protected static void ensureWorkday(Calendar cal) {
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        while (dow == Calendar.SATURDAY || dow == Calendar.SUNDAY) {
            cal.add(Calendar.DAY_OF_YEAR, 1);
            dow = cal.get(Calendar.DAY_OF_WEEK);
        }
    }
}

Related

  1. addWorkingDays(Date start, int workingDays)
  2. getRemainingWorkingMonth(Date taxdate)
  3. getWorkDay(Date d1, Date d2, int[] freeDays)
  4. isWorkDay(Date data)