Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8 Collectors.groupingBy

I want to groupingBy a Collection based on the LocalDateTime, but I only want to get the Hour, not the minutes, seconds...

.collect(Collectors.groupingBy(cp -> getUpdateLocalDate())


public LocalDate getUpdateLocalDate() {
    return getUpdateDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}

but I need also the Date, something like: 2018-10-12T09:00:00, not only the hour

like image 547
Nunyet de Can Calçada Avatar asked Oct 12 '18 09:10

Nunyet de Can Calçada


1 Answers

note that LocalDate doesn't have a time-zone. instead, return a LocalDateTime from the method getUpdateLocalDate (might be worth renaming also) then truncate the LocalDateTime instance.

 .collect(Collectors.groupingBy(cp -> cp.getUpdateLocalDateTime().truncatedTo(ChronoUnit.HOURS));
like image 117
Ousmane D. Avatar answered Jan 02 '23 00:01

Ousmane D.