码迷,mamicode.com
首页 > 编程语言 > 详细

java 小时时间就近取整

时间:2020-04-01 12:45:10      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:ram   next   trace   str   cat   java   pre   try   sim   

/**
* 时间就近取整
* 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<String> inTime = new ArrayList<String>();
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;
}

java 小时时间就近取整

标签:ram   next   trace   str   cat   java   pre   try   sim   

原文地址:https://www.cnblogs.com/zhangheliang/p/12611683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!