I'm working with some legacy code and I'm iterating through some values, the legacy code is checking for the start of a new day using an sql.timestamp value:
public static final long MILLIS_PER_SECOND = 1000;
public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;
if (entry.getPeriodEnd().getTime() % TimestampUtils.MILLIS_PER_DAY == 0 || i >= throughputEntries.size()) {
...
}
The result is never 0
so it only goes into the if
when i >= throughputEntries.size()
I can't get me head around how he was getting a 0
result, possibly his data was different and always ended with a certain time period that would produce the 0
I could rewrite the code maybe to do a check on the date, but would like to know how he achieved it with his code.
I'm not sure that this could be solved with the code I have provided, but maybe I'm missing something...
Edit
So I got my head around this in the end, thanks to @t0r0X & @Leo
I've modified my code to use Joda-Time as follows:
LocalDate newDay = null;
int count = 0;
public void calcAvg(){
DateTimeZone zone = DateTimeZone.getDefault();
LocalDate throughtputDate = entry.getPeriodEnd().toDateTime(zone).toLocalDate();
if ( this.newDay(throughtputDate) || i >= throughputEntries.size()) {
...
}
}
public boolean newDay(LocalDate date){
boolean go = false;
if(count !=0){
if(date.isAfter(newDay)){
newDay = date;
go = true;
}
}
else{
newDay = date;
count++;
}
return go;
}
Probably a cleaner way of checking for the new date than the method I've written, but time waits for no man.