package com.puhui.xiaojin.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
/**
* @author Created By Matteo
* @date 2020/12/5 18:11
* @Description: 时间格式化
*/
public class DateUtils {
/**
* HH:MM:SS
*/
public static boolean checkTimeHHMMSS(String time){
if (checkHHMMSS(time)){
String[] temp = time.split(":");
if (temp[0].length()==2||temp[0].length()==1&&temp[1].length()==2&&temp[2].length()==2){
int h,m,s;
h = Integer.parseInt(temp[0]);
m = Integer.parseInt(temp[1]);
s = Integer.parseInt(temp[2]);
if (h>=0 && h<=24&& m<=60 && m>=0 && s>=0 && s<=60){
return true;
}
}
}
return false;
}
/**
* 校验时间格式(仅格式)
* @param time
* @return
*/
private static boolean checkHHMMSS(String time) {
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
try {
Date parse = formatter.parse(time);
} catch (ParseException e) {
return false;
}
return true;
}
}
仅时分秒类型验证及转换(HH:MM:SS)
时间格式验证
最新推荐文章于 2022-12-02 16:09:52 发布
本文介绍了一个用于验证时间格式是否为 HH:MM:SS 的实用工具类。该工具通过 SimpleDateFormat 对象来解析时间字符串,并检查小时、分钟和秒是否符合标准的时间范围。
6543

被折叠的 条评论
为什么被折叠?



