diff --git a/source/java/org/alfresco/repo/usage/RepoUsageComponentImpl.java b/source/java/org/alfresco/repo/usage/RepoUsageComponentImpl.java index 36d673d373..f61befd6a3 100644 --- a/source/java/org/alfresco/repo/usage/RepoUsageComponentImpl.java +++ b/source/java/org/alfresco/repo/usage/RepoUsageComponentImpl.java @@ -52,7 +52,8 @@ import org.alfresco.service.cmr.attributes.AttributeService; import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.security.AuthorityService; import org.alfresco.service.namespace.QName; -import org.alfresco.service.transaction.TransactionService; +import org.alfresco.service.transaction.TransactionService; +import org.alfresco.util.DateUtil; import org.alfresco.util.PropertyCheck; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -512,24 +513,23 @@ public class RepoUsageComponentImpl implements RepoUsageComponent Long licenseExpiryDate = restrictions.getLicenseExpiryDate(); if (licenseExpiryDate != null) { - long remainingMs = licenseExpiryDate - System.currentTimeMillis(); - double remainingDays = (double) remainingMs / (double)(24*3600000); - if (remainingDays <= 0.0) + int remainingDays = DateUtil.calculateDays(System.currentTimeMillis(), licenseExpiryDate); + if (remainingDays <= 0) { errors.add(I18NUtil.getMessage("system.usage.err.limit_license_expired")); level = RepoUsageLevel.LOCKED_DOWN; } - else if (remainingDays <= 7.0) + else if (remainingDays <= 7) { - warnings.add(I18NUtil.getMessage("system.usage.err.limit_license_expiring", (int)remainingDays)); + warnings.add(I18NUtil.getMessage("system.usage.err.limit_license_expiring", remainingDays)); if (level.ordinal() < RepoUsageLevel.WARN_ADMIN.ordinal()) { level = RepoUsageLevel.WARN_ALL; } } - else if (remainingDays <= 21.0) + else if (remainingDays <= 21) { - warnings.add(I18NUtil.getMessage("system.usage.err.limit_license_expiring", (int)remainingDays)); + warnings.add(I18NUtil.getMessage("system.usage.err.limit_license_expiring", remainingDays)); if (level.ordinal() < RepoUsageLevel.WARN_ALL.ordinal()) { level = RepoUsageLevel.WARN_ADMIN; diff --git a/source/java/org/alfresco/util/DateUtil.java b/source/java/org/alfresco/util/DateUtil.java new file mode 100644 index 0000000000..963d5a1831 --- /dev/null +++ b/source/java/org/alfresco/util/DateUtil.java @@ -0,0 +1,74 @@ +/* + * #%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 . + * #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 default 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; + } +}