From 54ea6d3c0177a6c65720c6c0a83daeb2b374e33a Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Tue, 9 Feb 2016 16:14:39 +0100 Subject: [PATCH] Resolving bug when calculating the start of an UTC week Former-commit-id: 3e6b8124b4eecb0579894a78fe40715c3d2fe1ed --- src/se/hal/util/TimeUtility.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/se/hal/util/TimeUtility.java b/src/se/hal/util/TimeUtility.java index 513375bf..dc601849 100755 --- a/src/se/hal/util/TimeUtility.java +++ b/src/se/hal/util/TimeUtility.java @@ -1,6 +1,7 @@ package se.hal.util; import java.util.Calendar; +import java.util.TimeZone; public class TimeUtility { public static final long SECOND_IN_MS = 1000; @@ -12,10 +13,8 @@ public class TimeUtility { public static final long INFINITY = Long.MAX_VALUE; //sort of true public static long getTimestampPeriodStart_UTC(long periodLengthInMs, long timestamp) throws NumberFormatException{ - if(periodLengthInMs < 0 || timestamp < 0) - throw new NumberFormatException("argument must be positive"); - - return timestamp - (timestamp % periodLengthInMs); + //return timestamp - (timestamp % periodLengthInMs); + return getTimestampPeriodStart(periodLengthInMs, timestamp, Calendar.getInstance(TimeZone.getTimeZone("UTC"))); } /** @@ -25,10 +24,13 @@ public class TimeUtility { * @return */ public static long getTimestampPeriodStart_LOCAL(long periodLengthInMs, long timestamp) throws NumberFormatException{ + return getTimestampPeriodStart(periodLengthInMs, timestamp, Calendar.getInstance()); + } + + private static long getTimestampPeriodStart(long periodLengthInMs, long timestamp, Calendar cal) throws NumberFormatException{ if(periodLengthInMs < 0 || timestamp < 0) throw new NumberFormatException("argument must be positive"); - Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(timestamp); boolean clear = false;