Skip to content
Snippets Groups Projects
Commit cee342df authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Added a couple of new methods

parent 90b65dbc
No related branches found
No related tags found
No related merge requests found
...@@ -23,7 +23,7 @@ import java.util.Calendar; ...@@ -23,7 +23,7 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
/** /**
* @copyright 2017 <a href="http://www.nibio.no/">NIBIO</a> * @copyright 2018 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no> * @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/ */
public class DateUtil { public class DateUtil {
...@@ -59,4 +59,40 @@ public class DateUtil { ...@@ -59,4 +59,40 @@ public class DateUtil {
retVal += toDOY; retVal += toDOY;
return retVal; return retVal;
} }
public Date getClosestFutureWholeHour(Date timeStamp)
{
Calendar cal = Calendar.getInstance();
cal.setTime(timeStamp);
// If we're already at whole hour, change nothing
if(cal.get(Calendar.MINUTE) == 0 && cal.get(Calendar.SECOND) == 0 && cal.get(Calendar.MILLISECOND) == 0)
{
return timeStamp;
}
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + 1);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public Date getClosestPastWholeHour(Date timeStamp)
{
Calendar cal = Calendar.getInstance();
cal.setTime(timeStamp);
// If we're already at whole hour, change nothing
if(cal.get(Calendar.MINUTE) == 0 && cal.get(Calendar.SECOND) == 0 && cal.get(Calendar.MILLISECOND) == 0)
{
return timeStamp;
}
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment