如何在流(java)中添加dateTime值? [英] How to add dateTime values in a stream(java)?
问题描述
今天我开始学习Java 8,所以我对Java 8很陌生。
我有一份活动清单。活动具有name,startTime,endTime。对于startTime和endTime,我正在使用joda时间的DateTime。
Today I started learning Java 8, so I'm quite new to Java 8 and it's stuff. I have a list of activities. An activity has name, startTime, endTime. For startTime and endTime i'm using DateTime from joda time.
我正在尝试确定映射$ b的表单Map(String,DateTime)的数据结构每个活动$ b在监控期内计算的总持续时间。
I'm trying to determine a data structure of the form Map (String, DateTime) that maps for each activity the total duration computed over the monitoring period.
以下是我的代码片段:
import java.util.ArrayList;
import java.util.Map;
import org.joda.time.DateTime;
class Activity {
public DateTime startTime;
public DateTime endTime;
public String activityLabel;
public Activity(DateTime startTime, DateTime endTime, String activityLabel) {
super();
this.startTime = startTime;
this.endTime = endTime;
this.activityLabel = activityLabel;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Activity> list = new ArrayList<>();
//adding activities to list
list.add(new Activity(new DateTime(2017, 5, 12, 13, 10, 0), new DateTime(2017, 5, 12, 13, 40, 0), "Showering"));
list.add(new Activity(new DateTime(2017, 5, 12, 13, 41, 0), new DateTime(2017, 5, 12, 14, 20, 0), "Cooking"));
list.add(new Activity(new DateTime(2017, 5, 12, 14, 20, 0), new DateTime(2017, 5, 12, 14, 50, 0), "Eating"));
list.add(new Activity(new DateTime(2017, 5, 12, 14, 51, 0), new DateTime(2017, 5, 12, 15, 30, 0), "Sleeping"));
list.add(new Activity(new DateTime(2017, 5, 12, 17, 22, 0), new DateTime(2017, 5, 12, 17, 30, 0), "Showering"));
list.add(new Activity(new DateTime(2017, 5, 12, 17, 41, 0), new DateTime(2017, 5, 12, 17, 50, 0), "Cooking"));
list.add(new Activity(new DateTime(2017, 5, 12, 17, 50, 0), new DateTime(2017, 5, 12, 18, 20, 0), "Eating"));
list.add(new Activity(new DateTime(2017, 5, 12, 18, 21, 0), new DateTime(2017, 5, 12, 20, 30, 0), "TV"));
//creating a stream from list
Map<String, DateTime> map;
// map = list.stream().collect(Collectors.groupingBy(s -> s.activityLabel, Collectors. ...
}
}
我不知道如何获得这个结果:(。我尝试了不同的方法,我无法知道如何获得每个活动的总持续时间...谢谢你帮助我!
I don't know how to obtain this result :(. I tried different methods and i couldn't find out how obtain the total duration for each activity... Thank you for helping me!
推荐答案
首先开始使用Java 8 DateTime API。有一个内置的 Duration
类。然后你可以使用:
First start to use Java 8 DateTime API. There is a built-in Duration
class. Then you can use:
Map<String, Duration> map = list.stream().collect(
Collectors.groupingBy(a -> a.activityLabel,
Collectors.reducing(Duration.ZERO,
a -> Duration.between(a.startTime, a.endTime).abs(), Duration::plus
)
));
编辑
EDIT
由于您要使用 Joda
,您可以获得相同的结果(使用期
而不是持续时间
):
Since you've to use Joda
you can get the same results with (using Period
instead of Duration
):
Map<String, Period> map = list.stream().collect(
Collectors.groupingBy(a -> a.activityLabel,
Collectors.reducing(Period.ZERO,
a -> new Period(a.startTime, a.endTime), Period::plus
)
));
这篇关于如何在流(java)中添加dateTime值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!