作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的程序需要计算 worker 的加类时间以用于计算工资。为此,我现在要做的就是获取 worker 的上下类时间,然后用公司的时间尺度进行计算,得到每个 worker 加类了多少小时。我需要将这些时间四舍五入到最接近的 15 分钟,因此我需要一门类(class)来传递时间并返回四舍五入的时间。
例如:
如果加类时间为 2.56 小时,则必须为 3 小时。
公共(public)字符串 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 "";
}
最佳答案
如果您可以从时间中提取分钟并将其传递给此函数:
public static int getNear15Minute(int minutes){
int mod = minutes%15;
int res = 0 ;
if((mod) >=8){
res = minutes+(15 - mod);
}else{
res = minutes-mod;
}
return res; //return rounded minutes
}
这里我假设大于或等于 8 的数字接近 15。
关于java - 如何在java中将时间四舍五入到最接近的15分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41438768/
我是一名优秀的程序员,十分优秀!