0
点赞
收藏
分享

微信扫一扫

java小工具util系列9:检测一个字符串是否是时间格式

在这里插入图片描述

解决方案

/**
*  检测一个字符串是否是时间格式
   * @param str 请求字符串
   * @author liudz
   * @date 2019/12/17
   * @return 执行结果
   **/
public static boolean isValidDate(String str) {
    boolean convertSuccess = true;
    // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
    SimpleDateFormat format = new SimpleDateFormat("HH:mm");
    try {
        format.setLenient(false);
        format.parse(str);
    } catch (Exception e) {
        convertSuccess = false;
    }
    return convertSuccess;
}
举报

相关推荐

0 条评论