大家好,又见面了,我是你们的朋友全栈君。
/**
* 时间就近取整
* 08:00 -> 08:00,
* 08:20 -> 08:30,
* 08:30 -> 08:30,
* 08:45 -> 09:00,
* 23:56 -> 00:00
*
* @param time
* @return outTime
*/
public static String getCompleteTime(String time) {
String hour = “00”;//小时
String minutes = “00”;//分钟
String outTime = “00:00”;
StringTokenizer st = new StringTokenizer(time, “:”);
List inTime = new ArrayList();
while (st.hasMoreElements()) {
inTime.add(st.nextToken());
}
hour = inTime.get(0).toString();
minutes = inTime.get(1).toString();
if (Integer.parseInt(minutes) > 30) {
hour = (Integer.parseInt(hour) + 1) + “”;
outTime = hour + “:00”;
SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm”);
try {
outTime = sdf.format(sdf.parse(outTime));
} catch (Exception e) {
e.printStackTrace();
}
} else if (Integer.parseInt(minutes) == 00) {
outTime = hour + “:00”;
SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm”);
try {
outTime = sdf.format(sdf.parse(outTime));
} catch (Exception e) {
e.printStackTrace();
}
} else if (Integer.parseInt(minutes) <= 30 && Integer.parseInt(minutes) != 00) {
outTime = hour + “:30”;
SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm”);
try {
outTime = sdf.format(sdf.parse(outTime));
} catch (Exception e) {
e.printStackTrace();
}
}
return outTime;
}
原文:https://www.cnblogs.com/zhangheliang/p/12611683.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/127493.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...