package senac_turma02.data;

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

public class DiaUtil extends Date {
    public DiaUtil() {
      this(System.currentTimeMillis());
    }
    public DiaUtil(long date) {
      setTime(date);
    }
    public DiaUtil(Date date) {
      this(date.getTime());
    }
    public void setTime(long time) {
      super.setTime(time);
      if (!isDiaUtil(this))
          setTime(proximoDiaUtil().getTime());
    }
    public static boolean isDiaUtil(Calendar cal) {
      return !(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
            || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);
    }
    public static boolean isDiaUtil(Date dia) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(dia);
      return isDiaUtil(cal);
    }
    public DiaUtil proximoDiaUtil() {
      return proximoDiaUtil(1);
    }
    public DiaUtil proximoDiaUtil(int nDias) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(this);
      for (int i = 0; i < nDias; i++)
          while (!isDiaUtil(cal))
            cal.add (Calendar.DAY_OF_MONTH, 1);
      return new DiaUtil(cal.getTime());
    }
}
