Here you can find the source of calculateTruncatedTime(final Date dateTimeBegin, final Date dateTimeEnd, final Date begin, final Date end)
Parameter | Description |
---|---|
dateTimeBegin | a parameter |
dateTimeEnd | a parameter |
begin | a parameter |
end | a parameter |
private static int calculateTruncatedTime(final Date dateTimeBegin, final Date dateTimeEnd, final Date begin, final Date end)
//package com.java2s; /**/*ww w .j a v a2s . com*/ * 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 { /** * 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; } }