Reputation: 1667
Compare two ZonedDateTime
with ignoring the Zone:
Example:
2022-08-17 03:30:00+02
compare 2022-08-17 03:30:00+01
must give true
I Have a stream that must compare each object with others using this Date:
Set<Tele> jPointsSet = jPoints.stream()
.collect(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(Tele::getDateM))));
the getDateM
is ZonedDateTime
so I would like when use Comparator.comparing(Tele::getDateM))
ignore the Zone
Upvotes: 0
Views: 711
Reputation: 28988
As @OH GOD SPIDERS said in the comments, you can extract LocalDateTime
from a ZonedDateTime
.
Comparator.comparing(tele -> tele.getDateM().toLocalDateTime())
But be aware that if two LocalDateTime
instances are equal, it doesn't necessarily mean that they're describing the same point in time. In order to establish comparison based on a specific point in the global timeline, you need to use Instant
.
Comparator.comparing(tele -> tele.getDateM().toInstant())
Sidenote: while dealing with a TreeSet
, it's more handy to use NavigableSet
interface as an abstract type. It gives you access to methods like pollFirst()
, pollLast()
, lower()
, higher()
, etc. which are not declared in the Set
interface.
Upvotes: 1