Reputation: 479
I want to get the first datetime of the month for the given date-time as follows: For the date 19-04-2018 19:00:00
(dd-MM-yyyy HH:mm:ss) it should be 01-04-2018 00:00:00
.
I have tried the below code, but it gives the empty list.
startDate
may contains the value like 01-04-2018 00:00:00
.
List<scPosR> scPosRListPerStoreAndTimeStamp = spst.stream().filter(
scPosR -> Timestamp.valueOf(scPosR.getTransactionDate()
.toLocalDateTime()
.toLocalDate()
.withDayOfMonth(1)
.atStartOfDay())
.equals(startDate))
.collect(Collectors.toList());
Can anyone please suggest me to do it?
Upvotes: 0
Views: 124
Reputation: 86324
I cannot tell what went wrong. Like Pavan observed, your code works as expected also on my computer and returns a list containing the objects with a transaction date in April. One possible explanation is if your startDate
variable doesn’t hold a Timestamp
, but some other (runtime) type. Another possible explanation is a timezone issue: if your startDate
timestamp was created in a different time zone, it won’t be equal to the timestamps created in your stream filter.
Anyway here’s a simpler version of your code hoping it will also be simpler to debug.
YearMonth month = YearMonth.of(2018, Month.APRIL);
List<ScPosR> scPosRListPerStoreAndTimeStamp = spst.stream()
.filter(scPosR -> YearMonth.from(scPosR.getTransactionDate().toLocalDateTime())
.equals(month))
.collect(Collectors.toList());
Still better if you could get completely rid of the outdated Timestamp
class, of course.
Upvotes: 1