I have a list of activities(Activity) and I want to determine a data structure of the form Map(String, DateTime)
(not Duration or Period; DateTime
it's a must) that maps. For each activity the total duration computed over the monitoring period.
The class Activity has: activityLabel(String)
, startTime(DateTime)
, endTime(DateTime)
. I use joda
time.
This is what I have done:
Map<String, DateTime> durations = activities.stream().collect(Collectors.toMap(
it -> it.activityLabel,
it ->new DateTime(0,0,0,0,0,0)
//,DateTime::plus
));
I guess I should use DateTime plus(ReadablePeriod period) or DateTime plus(ReadableDuration duration) , but I don't know how to send a parameter of type Duration or Period to the method reference.
How can I achieve this result?
EDIT: For the input:
2011-12-03 01:00:00 2011-12-03 9:00:00 Sleeping
2011-12-04 03:00:00 2011-12-04 10:30:00 Sleeping
I should have the output: Sleeping 0-0-0 15:30:00 (years,months,days,hours,minutes,seconds)
As you have mentioned in comment that you really need is a
DateTime
not aPeriod
.Since the DateTime has no api for
DateTime.plus(DateTime)
/DateTime.minus(DateTime)
, but you can plus / minus aPeriod
on aDateTime
, then you need aDateTime
to start, and the code using Collectors api is replacingtoMap
withgroupingBy
which is more efficiently and expressiveness for doing the task in your case:The code (using a Period) would look like this:
If you really want to output that
Period
as a String, you needPeriodFormatter
.And then your code would look more like this: