Java Timestamp inRange(Timestamp start, Timestamp end, boolean OnMonday, boolean OnTuesday, boolean OnWednesday, boolean OnThursday, boolean OnFriday, boolean OnSaturday, boolean OnSunday)

Here you can find the source of inRange(Timestamp start, Timestamp end, boolean OnMonday, boolean OnTuesday, boolean OnWednesday, boolean OnThursday, boolean OnFriday, boolean OnSaturday, boolean OnSunday)

Description

Is start..end on one of the days ?

License

Open Source License

Parameter

Parameter Description
start start day
end end day (not including)
OnMonday true if OK
OnTuesday true if OK
OnWednesday true if OK
OnThursday true if OK
OnFriday true if OK
OnSaturday true if OK
OnSunday true if OK

Return

true if on one of the days

Declaration

static public boolean inRange(Timestamp start, Timestamp end, boolean OnMonday, boolean OnTuesday,
        boolean OnWednesday, boolean OnThursday, boolean OnFriday, boolean OnSaturday, boolean OnSunday) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
 * Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
 * are Copyright (C) 1999-2005 Jorg Janke.
 * All parts are Copyright (C) 1999-2005 ComPiere, Inc.  All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/

import java.sql.Timestamp;

import java.util.BitSet;
import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    /**// w ww .  j  av a2 s. c  om
     *    Is the _1 in the Range of _2
     *  <pre>
     *       Time_1         +--x--+
     *       Time_2   +a+      +---b---+   +c+
     *    </pre>
     *  The function returns true for b and false for a/b.
     *  @param start_1 start (1)
     *  @param end_1 not included end (1)
     *  @param start_2 start (2)
     *  @param end_2 not included (2)
     *  @return true if in range
     */
    static public boolean inRange(Timestamp start_1, Timestamp end_1, Timestamp start_2, Timestamp end_2) {
        //   validity check
        if (end_1.before(start_1))
            throw new UnsupportedOperationException(
                    "TimeUtil.inRange End_1=" + end_1 + " before Start_1=" + start_1);
        if (end_2.before(start_2))
            throw new UnsupportedOperationException(
                    "TimeUtil.inRange End_2=" + end_2 + " before Start_2=" + start_2);
        //   case a
        if (!end_2.after(start_1)) //   end not including
        {
            //      log.fine( "TimeUtil.InRange - No", start_1 + "->" + end_1 + " <??> " + start_2 + "->" + end_2);
            return false;
        }
        //   case c
        if (!start_2.before(end_1)) //    end not including
        {
            //      log.fine( "TimeUtil.InRange - No", start_1 + "->" + end_1 + " <??> " + start_2 + "->" + end_2);
            return false;
        }
        //   log.fine( "TimeUtil.InRange - Yes", start_1 + "->" + end_1 + " <??> " + start_2 + "->" + end_2);
        return true;
    }

    /**
     *    Is start..end on one of the days ?
     *  @param start start day
     *  @param end end day (not including)
     *  @param OnMonday true if OK
     *  @param OnTuesday true if OK
     *  @param OnWednesday true if OK
     *  @param OnThursday true if OK
     *  @param OnFriday true if OK
     *  @param OnSaturday true if OK
     *  @param OnSunday true if OK
     *  @return true if on one of the days
     */
    static public boolean inRange(Timestamp start, Timestamp end, boolean OnMonday, boolean OnTuesday,
            boolean OnWednesday, boolean OnThursday, boolean OnFriday, boolean OnSaturday, boolean OnSunday) {
        //   are there restrictions?
        if (OnSaturday && OnSunday && OnMonday && OnTuesday && OnWednesday && OnThursday && OnFriday)
            return false;

        GregorianCalendar calStart = new GregorianCalendar();
        calStart.setTimeInMillis(start.getTime());
        int dayStart = calStart.get(Calendar.DAY_OF_WEEK);
        //
        GregorianCalendar calEnd = new GregorianCalendar();
        calEnd.setTimeInMillis(end.getTime());
        calEnd.add(Calendar.DAY_OF_YEAR, -1); //   not including
        int dayEnd = calEnd.get(Calendar.DAY_OF_WEEK);

        //   On same day
        if (calStart.get(Calendar.YEAR) == calEnd.get(Calendar.YEAR)
                && calStart.get(Calendar.MONTH) == calEnd.get(Calendar.MONTH)
                && calStart.get(Calendar.DAY_OF_MONTH) == calEnd.get(Calendar.DAY_OF_YEAR)) {
            if ((!OnSaturday && dayStart == Calendar.SATURDAY) || (!OnSunday && dayStart == Calendar.SUNDAY)
                    || (!OnMonday && dayStart == Calendar.MONDAY) || (!OnTuesday && dayStart == Calendar.TUESDAY)
                    || (!OnWednesday && dayStart == Calendar.WEDNESDAY)
                    || (!OnThursday && dayStart == Calendar.THURSDAY)
                    || (!OnFriday && dayStart == Calendar.FRIDAY)) {
                //      log.fine( "TimeUtil.InRange - SameDay - Yes", start + "->" + end + " - "
                //         + OnMonday+"-"+OnTuesday+"-"+OnWednesday+"-"+OnThursday+"-"+OnFriday+"="+OnSaturday+"-"+OnSunday);
                return true;
            }
            //   log.fine( "TimeUtil.InRange - SameDay - No", start + "->" + end + " - "
            //      + OnMonday+"-"+OnTuesday+"-"+OnWednesday+"-"+OnThursday+"-"+OnFriday+"="+OnSaturday+"-"+OnSunday);
            return false;
        }
        //
        //   log.fine( "TimeUtil.inRange - WeekDay Start=" + dayStart + ", Incl.End=" + dayEnd);

        //   Calendar.SUNDAY=1 ... SATURDAY=7
        BitSet days = new BitSet(8);
        //   Set covered days in BitArray
        if (dayEnd <= dayStart)
            dayEnd += 7;
        for (int i = dayStart; i < dayEnd; i++) {
            int index = i;
            if (index > 7)
                index -= 7;
            days.set(index);
            //      System.out.println("Set index=" + index + " i=" + i);
        }

        //   for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++)
        //      System.out.println("Result i=" + i + " - " + days.get(i));

        //   Compare days to availability
        if ((!OnSaturday && days.get(Calendar.SATURDAY)) || (!OnSunday && days.get(Calendar.SUNDAY))
                || (!OnMonday && days.get(Calendar.MONDAY)) || (!OnTuesday && days.get(Calendar.TUESDAY))
                || (!OnWednesday && days.get(Calendar.WEDNESDAY)) || (!OnThursday && days.get(Calendar.THURSDAY))
                || (!OnFriday && days.get(Calendar.FRIDAY))) {
            //      log.fine( "MAssignment.InRange - Yes",   start + "->" + end + " - "
            //         + OnMonday+"-"+OnTuesday+"-"+OnWednesday+"-"+OnThursday+"-"+OnFriday+"="+OnSaturday+"-"+OnSunday);
            return true;
        }

        //   log.fine( "MAssignment.InRange - No", start + "->" + end + " - "
        //      + OnMonday+"-"+OnTuesday+"-"+OnWednesday+"-"+OnThursday+"-"+OnFriday+"="+OnSaturday+"-"+OnSunday);
        return false;
    }
}

Related

  1. getTodayTimestamp()
  2. getUtilDate(java.sql.Timestamp time)
  3. getValue(Timestamp t)
  4. getZonedDateTime(final Timestamp timestamp)
  5. hasTimeExpired(Timestamp referenceTime)
  6. internalToTimestamp(long v)
  7. isAllDay(Timestamp start, Timestamp end)
  8. isDifferentDay(final Timestamp ts1, final Timestamp ts2)
  9. isDST(Timestamp ts)