将小时和分钟相加会导致错误的答案。
这里有 2 个带小时的列表。
[[0小时:0分钟:0秒,1小时:16分钟:22秒,0小时:1分钟:53秒,23小时:54分钟:18秒],[0小时:8分钟:22秒,0小时:8 分钟:22 秒,0 小时:8 分钟:22 秒]]这是 HOURS
然后,我将它分成子列表,为每个子列表计算。这是子列表之一。
[0 小时 0 分钟 0 秒 1 小时 16 分钟 22 秒 0 小时 1 分钟 53 秒 23 小时 54 分钟 18 秒]
所以,我得到的这个子列表的答案是:1 小时:12 分钟:33 秒,这是不正确的。我想大约需要 25 小时加上几分钟。
public List<String> getTheHoursWorked() {
DateTimeFormatter parser = DateTimeFormatter.ofPattern("H 'hours :' m 'mins :' s 'sec'", Locale.US);
final DateFormat dt = new SimpleDateFormat("HH:mm:ss");
final Calendar c = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
c.clear();
long startingMS = c.getTimeInMillis();
int counter = 0;
for (int k = 0; k < hours.size(); k++){
List<Object> shorter = new ArrayList<>();
List<Object> temp;
temp = (List<Object>) hours.get(k);
long milliseconds = 0;
for (int m = 0; m < shorter.size(); m++) {
LocalTime odt = LocalTime.parse((CharSequence) shorter.get(m), parser);
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("HH:mm:ss");
String printDate = formatter.format(odt);
try {
milliseconds = milliseconds + (dt.parse(printDate).getTime() - startingMS);
System.out.println(milliseconds + "MILISECONDS");
} catch (ParseException e) {
e.printStackTrace();
}
}
hoursToString.add(String.valueOf(shorter));
String s = milliseconds / 1000 % 60 + " seconds";
String m = milliseconds /(60 * 1000) % 60 + " minutes";
String h = milliseconds / (60 * 60 * 1000) % 24 + " hours";
String together = h+":"+m+":"+s;
togetherHours.add(together);
}
return togetherHours;
}