/**
* 時間丸め処理
* @param time 時間
* @param roundMinutes 丸め分数
* @param roundMethod 丸め方法
* @return 丸め後時間
*/
public static String timeRound(String time, Integer roundMinutes, String roundMethod) {
// 丸め分数かどうか丸め方法が、空の場合
if (StringUtils.isEmpty(time) || roundMinutes == null
|| StringUtils.isEmpty(roundMethod) || roundMinutes == 0) {
return time;
}
// 時間タイプの変換
Date timeTemp = DateUtil.convStr2Date(time, Const.DATE_FORMAT_DB_HHMM);
// 時
int hour = Integer.parseInt(DateUtil.convDate2Str(timeTemp, "HH"));
// 分
int minutes = Integer.parseInt(DateUtil.convDate2Str(timeTemp, "mm"));
// 計算分数
double countMin = hour * 60 + minutes;
// 分数単位
double unit = 0;
// 切り上げの‘場合
if (Const.ROUND_METHOD_UP.equals(roundMethod)) {
// 上げの丸め
unit = Math.ceil(countMin/roundMinutes);
} else {
// 捨ての丸め
unit = Math.floor(countMin/roundMinutes);
}
double temp = roundMinutes * unit / 60;
// 丸め後、時を取得する
hour = (int) temp;
// 丸め後、分を取得する
minutes = (int) Math.round(((temp - hour) * 60));
// 丸め後、時間を設定する
time = CommonUtil.formatLeftZero2(hour) + CommonUtil.formatLeftZero2(minutes);
return time;
}
時間丸め処理
最新推荐文章于 2025-03-10 20:17:27 发布