Java Date Calculate calculateTruncatedTimeByDays( final Date dateTimeStart, final Date dateTimeEnd, final Date start, final Date end)

Here you can find the source of calculateTruncatedTimeByDays( final Date dateTimeStart, final Date dateTimeEnd, final Date start, final Date end)

Description

Retrieves time of activity on period.

License

Open Source License

Parameter

Parameter Description
dateTimeStart a parameter
dateTimeEnd a parameter
start a parameter
end a parameter

Declaration

public static int calculateTruncatedTimeByDays(
        final Date dateTimeStart, final Date dateTimeEnd,
        final Date start, final Date end) 

Method Source Code

//package com.java2s;
/**//from  www . j a  v  a  2s . c o m
 * Vulpe Framework - Quick and Smart ;)
 * Copyright (C) 2011 Active Thread
 *
 * Este programa ? software livre; voc? pode redistribu?-lo e/ou
 * modific?-lo sob os termos da Licen?a P?blica Geral GNU, conforme
 * publicada pela Free Software Foundation; tanto a vers?o 2 da
 * Licen?a como (a seu crit?rio) qualquer vers?o mais nova.
 *
 * Este programa ? distribu?do na expectativa de ser ?til, mas SEM
 * QUALQUER GARANTIA; sem mesmo a garantia impl?cita de
 * COMERCIALIZA??O ou de ADEQUA??O A QUALQUER PROP?SITO EM
 * PARTICULAR. Consulte a Licen?a P?blica Geral GNU para obter mais
 * detalhes.
 *
 * Voc? deve ter recebido uma c?pia da Licen?a P?blica Geral GNU
 * junto com este programa; se n?o, escreva para a Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

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

public class Main {
    /**
     * Retrieves time of activity on period.
     *
     * @param dateTimeStart
     * @param dateTimeEnd
     * @param start
     * @param end
     * @return
     */
    public static int calculateTruncatedTimeByDays(
            final Date dateTimeStart, final Date dateTimeEnd,
            final Date start, final Date end) {
        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);
            final Date datePeriodStart = calendarBegin.getTime();

            final Calendar calendarEnd = new GregorianCalendar();
            calendarEnd.setTime(periodEnd);
            calendarEnd.set(Calendar.DAY_OF_MONTH,
                    calendarBegin.get(Calendar.DAY_OF_MONTH));
            calendarEnd.set(Calendar.MONTH,
                    calendarBegin.get(Calendar.MONTH));
            calendarEnd
                    .set(Calendar.YEAR, calendarBegin.get(Calendar.YEAR));
            final Date datePeriodEnd = calendarEnd.getTime();

            Date dtBeginActivity = startActivity;
            Date dtEndActivity = endActivity;
            final Calendar dayEndStart = new GregorianCalendar();
            final Calendar beginNextDay = new GregorianCalendar();
            do {
                dayEndStart.setTime(dtBeginActivity);
                dayEndStart.set(Calendar.HOUR_OF_DAY, 23);
                dayEndStart.set(Calendar.MINUTE, 59);
                dayEndStart.set(Calendar.SECOND, 59);
                dayEndStart.add(Calendar.SECOND, 1);
                final Date dTimeEnd = dayEndStart.getTime();
                if (endActivity.compareTo(dTimeEnd) > 0) {
                    dtEndActivity = dTimeEnd;
                } else {
                    dtEndActivity = endActivity;
                }
                final int parcialMinutes = calculateTruncatedTime(
                        dtBeginActivity, dtEndActivity, datePeriodStart,
                        datePeriodEnd);
                minutes += parcialMinutes;
                beginNextDay.setTime(dtEndActivity);
                dtBeginActivity = beginNextDay.getTime();
            } while (dtEndActivity.compareTo(endActivity) != 0);
        }
        return minutes;
    }

    /**
     * Calculate quantity of minutes of task.
     *
     * @param dateTimeBegin
     * @param dateTimeEnd
     * @param begin
     * @param end
     * @return
     */
    private static int calculateTruncatedTime(final Date dateTimeBegin,
            final Date dateTimeEnd, final Date begin, final Date end) {

        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());
            calendarEnd2.add(Calendar.DAY_OF_YEAR, 1);

            return calculateTruncatedTime(dateTimeBegin, dateTimeEnd,
                    calendarBegin1.getTime(), end)
                    + calculateTruncatedTime(dateTimeBegin, dateTimeEnd,
                            begin, calendarEnd2.getTime());
        }

        int returnedTime = 0;
        final Calendar periodBegin = new GregorianCalendar();
        periodBegin.setTime(begin);
        final Calendar periodEnd = new GregorianCalendar();
        periodEnd.setTime(end);
        final Calendar startActivity = new GregorianCalendar();
        startActivity.setTime(dateTimeBegin);

        periodBegin.set(startActivity.get(Calendar.YEAR),
                startActivity.get(Calendar.MONTH),
                startActivity.get(Calendar.DATE));
        periodEnd.set(startActivity.get(Calendar.YEAR),
                startActivity.get(Calendar.MONTH),
                startActivity.get(Calendar.DATE));

        if (periodBegin.getTime().compareTo(periodEnd.getTime()) > 0) {
            periodEnd.add(Calendar.DATE, 1);
        }
        final Date start = periodBegin.getTime();
        final Date finish = periodEnd.getTime();

        final int period = getMinutesDifference(start, finish);
        final int activityTime = getMinutesDifference(dateTimeBegin,
                dateTimeEnd);

        if (dateTimeBegin.compareTo(start) < 0) {
            returnedTime = Math.max(Math.min(
                    getMinutesDifference(start, dateTimeEnd), period), 0);
        } else if (dateTimeBegin.compareTo(finish) > 0) {
            periodBegin.add(Calendar.DATE, 1);
            returnedTime = Math.max(
                    Math.min(
                            getMinutesDifference(periodBegin.getTime(),
                                    dateTimeEnd), period), 0);
        } else if (dateTimeEnd.compareTo(finish) < 0) {
            returnedTime = activityTime;
        } else {
            periodBegin.add(Calendar.DATE, 1);
            if (dateTimeEnd.compareTo(periodBegin.getTime()) > 0) {
                returnedTime = activityTime - period;
            } else {
                returnedTime = getMinutesDifference(dateTimeBegin, finish);
            }
        }
        return returnedTime;
    }

    /**
     * Calculate diff in minutes between dates.
     *
     * @param begin
     * @param end
     * @return
     */
    public static int getMinutesDifference(final Date begin, final Date end) {
        return (int) getMillisecondDifference(begin, end) / 60000;
    }

    /**
     * Calculate diff in milliseconds between dates.
     *
     * @param begin
     * @param end
     * @return
     */
    public static long getMillisecondDifference(final Date begin,
            final Date end) {
        long milliseconds;

        final Calendar dtBegin = new GregorianCalendar();
        final Calendar dtEnd = new GregorianCalendar();

        dtBegin.setTime(begin);
        dtBegin.set(Calendar.HOUR_OF_DAY, dtBegin.get(Calendar.HOUR_OF_DAY));
        dtBegin.set(Calendar.MINUTE, dtBegin.get(Calendar.MINUTE));
        dtBegin.set(Calendar.SECOND, dtBegin.get(Calendar.SECOND));
        dtBegin.set(Calendar.MILLISECOND, dtBegin.get(Calendar.MILLISECOND));

        dtEnd.setTime(end);
        dtEnd.set(Calendar.HOUR_OF_DAY, dtEnd.get(Calendar.HOUR_OF_DAY));
        dtEnd.set(Calendar.MINUTE, dtEnd.get(Calendar.MINUTE));
        dtEnd.set(Calendar.SECOND, dtEnd.get(Calendar.SECOND));
        dtEnd.set(Calendar.MILLISECOND, dtEnd.get(Calendar.MILLISECOND));

        milliseconds = dtEnd.getTime().getTime()
                - dtBegin.getTime().getTime();
        return milliseconds;
    }
}

Related

  1. calculatePastDate(int days)
  2. calculateTime(Date date, String addpart, int num)
  3. calculateTime(Date sendTime, String timeZoneID)
  4. calculateTimestamp()
  5. calculateTruncatedTime(final Date dateTimeBegin, final Date dateTimeEnd, final Date begin, final Date end)
  6. calculationAge(Date birthday, Date calculateDay)
  7. getDateCalculate(int d)