Files
alfresco-community-repo/source/java/org/alfresco/util/DateUtil.java
Alex Mukha 89f8a6c95a Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
126058 jkaabimofrad: ACE-5001: Added a utility class to calculate the number of days between the start and end dates, so we could have a single source when dealing with such functionality. Also, changed the RepoUsageComponentImpl class to use this util class.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126098 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-05-04 14:07:46 +00:00

75 lines
2.4 KiB
Java

/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.util;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.joda.time.PeriodType;
/**
* A utility class for working with dates.
*
* @author Jamal Kaabi-Mofrad
*/
public class DateUtil
{
private DateUtil()
{
//Private constructor to suppress default constructor for non-instantiability
}
/**
* Calculate the number of days between start and end dates based on the <b>default</b> timezone.
* If the end date is before the start date, the returned value is negative.
*
* @param startMs start date in milliseconds
* @param endMs end date in milliseconds
* @return number days between
*/
public static int calculateDays(long startMs, long endMs)
{
DateTime startDateTime = new DateTime(startMs).withTimeAtStartOfDay();
DateTime endDateTime = new DateTime(endMs).withTimeAtStartOfDay();
int days;
if (endDateTime.isBefore(startDateTime))
{
Interval interval = new Interval(endDateTime, startDateTime);
Period period = interval.toPeriod(PeriodType.days());
days = 0 - period.getDays();
}
else
{
Interval interval = new Interval(startDateTime, endDateTime);
Period period = interval.toPeriod(PeriodType.days());
days = period.getDays();
}
return days;
}
}