我的程序需要计算工作人员的工资计算用加班时间。为此,我现在正在做的工作是让工人的进入时间和时间,然后根据公司的时间表计算出每个工人加班多少小时。我需要将这些时间舍入到最接近的15分钟,所以我需要一个班级在小时内通过并返回四舍五入的小时数。如何在java中将时间缩短到最接近的15分钟
例如:
- 如果加班时间是2.17小时,它必须被舍入到2.15。
- 如果加班时间是2.24小时,它必须四舍五入到2.30。
- 如果加班时间是2.40小时,它必须是2.45。
如果加班时间是2.56小时,它必须是3小时。
公共字符串RoundToNearest15Min(长时间){
long milsec = Time; System.out.println("milsecond :" + milsec); long sec = Time/1000; ValidateValues.roundupDouble(sec); System.out.println("second :" + sec); double min = sec/60; ValidateValues.roundupDouble(min); System.out.println("miniutes :" + min); double hrs = min/60; System.out.println("hours :" + hrs); double roundeVal = ValidateValues.roundupDouble(hrs); String hrsvalue = String.valueOf(roundeVal); System.out.println(hrsvalue); String splitVal[] = hrsvalue.split("\\."); System.out.println(splitVal[0]); System.out.println(splitVal[1]); int splitedValue2 = Integer.parseInt(splitVal[1]); int splitedValue1 = Integer.parseInt(splitVal[0]); if (splitedValue2 <= 15) { int rounded = splitedValue2 > 7 ? (15) : (0); return splitedValue1 + "." + rounded; } if (splitedValue2 > 15 && splitedValue2 <= 30) { int rounded = splitedValue2 > 22 ? (30) : (15); return splitedValue1 + "." + rounded; } if (splitedValue2 > 30 && splitedValue2 <= 45) { int rounded = splitedValue2 > 37 ? (45) : (30); return splitedValue1 + "." + rounded; } if (splitedValue2 > 45 && splitedValue2 <= 59) { int rounded = splitedValue2 > 51 ? (00) : (45); if (rounded == 00) { splitedValue1++; return splitedValue1 + "." + rounded; } else { return splitedValue1 + "." + rounded; } } return "";
}
我使用了一些计算,但没有锻炼它们。 – user2576816
什么是这里使用的“时间实例”,“long epoch”,“Date”,“LocalDate”,“Calendar”,...四舍五入通常使用模运算符完成。显示你有什么,以帮助你在那一部分 – AxelH
这是现在即时通讯使用的类, – user2576816