Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DateTimeType Comparable #4364

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@
* @author Wouter Born - increase parsing and formatting precision
* @author Laurent Garnier - added methods toLocaleZone and toZone
* @author Gaël L'hopital - added ability to use second and milliseconds unix time
* @author Jimmy Tanagra - implement Comparable
*/
@NonNullByDefault
public class DateTimeType implements PrimitiveType, State, Command {
public class DateTimeType implements PrimitiveType, State, Command, Comparable<DateTimeType> {

// external format patterns for output
public static final String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
Expand Down Expand Up @@ -229,6 +230,11 @@ public boolean equals(@Nullable Object obj) {
return zonedDateTime.compareTo(other.zonedDateTime) == 0;
}

@Override
public int compareTo(DateTimeType o) {
return zonedDateTime.compareTo(o.getZonedDateTime());
}

private ZonedDateTime parse(String value) throws DateTimeParseException {
ZonedDateTime date;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ public void equalityTest() {
assertTrue(dt1.equals(dt2));
}

@Test
public void comparabilityTest() {
ZonedDateTime zoned = ZonedDateTime.now();
DateTimeType dt1 = new DateTimeType(zoned);
DateTimeType dt2 = new DateTimeType(zoned.plusSeconds(1));
DateTimeType dt3 = new DateTimeType(zoned.minusSeconds(1));

assertTrue(dt1.compareTo(dt2) < 0);
assertTrue(dt1.compareTo(dt3) > 0);
assertTrue(dt1.compareTo(dt1) == 0);
}

@Test
public void zonedParsingTest() {
DateTimeType dt1 = new DateTimeType("2019-06-12T17:30:00Z");
Expand Down