Skip to content
Snippets Groups Projects

Misc changes pre 0.7.8

3 files
+ 199
167
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -16,7 +16,10 @@ import java.time.ZoneOffset;
@@ -16,7 +16,10 @@ import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.DateTimeParseException;
 
import java.time.temporal.ChronoUnit;
 
import java.time.temporal.TemporalUnit;
import java.util.Date;
import java.util.Date;
 
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.LoggerFactory;
@@ -33,8 +36,8 @@ public class DateTimeHelper {
@@ -33,8 +36,8 @@ public class DateTimeHelper {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(RAW_RFC_3339_FORMAT);
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(RAW_RFC_3339_FORMAT);
/**
/**
* Converts RFC 3339 compliant date string to date object. If non compliant string is passed, issue is logged and
* Converts RFC 3339 compliant date string to date object. If non compliant string is passed, issue is logged and null
* null is returned. If negative UTC timezone (-00:00) is passed, UTC time zone is assumed.
* is returned. If negative UTC timezone (-00:00) is passed, UTC time zone is assumed.
*
*
* @param dateString an RFC 3339 date string.
* @param dateString an RFC 3339 date string.
* @return a date object representing time in date string, or null if not in RFC 3339 format.
* @return a date object representing time in date string, or null if not in RFC 3339 format.
@@ -90,6 +93,36 @@ public class DateTimeHelper {
@@ -90,6 +93,36 @@ public class DateTimeHelper {
return time.toInstant().toEpochMilli();
return time.toInstant().toEpochMilli();
}
}
 
/**
 
* Compares 2 zoned date time objects, accurate to milliseconds.
 
*
 
* @param first the first date to compare
 
* @param second the second date to compare
 
* @return true if the dates represent the same time to the millisecond.
 
*/
 
public static boolean looseCompare(ZonedDateTime first, ZonedDateTime second) {
 
return looseCompare(first, second, Optional.of(ChronoUnit.MILLIS));
 
}
 
 
/**
 
* Compares 2 zoned date time objects, using logic to truncate to a common accuracy point. This may be needed for cases
 
* where the upstream data is only accurate to seconds rather than milliseconds.
 
*
 
* @param first the first date to compare
 
* @param second the second date to compare
 
* @param accuracy the max level of accuracy to use in comparisons, defaults to milliseconds
 
* @return true if the dates represent the same time to the given accuracy, or to millis if none is provided. Otherwise,
 
* returns false.
 
*/
 
public static boolean looseCompare(ZonedDateTime first, ZonedDateTime second, Optional<TemporalUnit> accuracy) {
 
return (first == null && second == null) || (first != null && second != null
 
&& first
 
.withZoneSameInstant(ZoneOffset.UTC)
 
.toInstant()
 
.truncatedTo(accuracy.orElse(ChronoUnit.MILLIS))
 
.compareTo(second.toInstant().truncatedTo(accuracy.orElse(ChronoUnit.MILLIS))) == 0);
 
}
 
// hide constructor
// hide constructor
private DateTimeHelper() {
private DateTimeHelper() {
}
}
Loading