List Chart charts = new ArrayList (); SimpleDateFormat format = newSimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ); charts . add ( new Chart ( format . parse ( "2020-01-01 01:10:00" ), 2 d )); charts . add ( new Chart ( format . parse ( "2020-01
SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
charts.add(new Chart(format.parse("2020-01-01 01:10:00"), 2d));
charts.add(new Chart(format.parse("2020-01-01 01:20:00"), 3d));
charts.add(new Chart(format.parse("2020-01-01 01:40:00"), 4d));
charts.add(new Chart(format.parse("2020-02-01 01:50:00"), 3d));
charts.add(new Chart(format.parse("2020-01-01 02:20:00"), 26d));
Map<Long, Chart> collect = charts.stream().collect(Collectors.toMap(chart ->
{
return chart.date.getTime() / (30 * 60 * 1000);
}, Function.identity(), (k1, k2)
-> {
return k1.date.getTime() > k2.date.getTime() ? k1 : k2;
}));
Map<String, Double> map = new HashMap<>();
format = new
SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat finalFormat = format;
collect.forEach((k, v) -> { //此处把v.date转换为半小时的标准格式
String format1 = finalFormat.format(v.date);
String hours = v.date.getHours() + "";
int minutes = v.date.getMinutes();
if
(minutes >= 0 && minutes < 30) {
hours = format1 + " " + hours + ":00";
} else {
hours = format1 + " " + hours + ":30";
}
map.put(hours, v.value);
});
Map<String, Double> stringDoubleMap = sortByKey(map);
static class Chart {
public Date date;
public Double value;
public Chart(Date date, Double value) {
this.date = date;
this.value = value;
}
}