您的位置 首页 java

java判断字符串是否是日期

Java为了支持多语言,没有固定的日期格式。你需要根据自己的需要指定日期格式,然后用DateFormat类或者SimpleDateFormat类来判断是否是正确的日期格式。

下面的例子供参考:

public class DateUtil {    private static final SimpleDateFormat dateFormat = null;    static {        dateFormat = new SimpleDateFormat("yyyy/MM/dd");        dateFormat.setLenient(false);    }    public static boolean isValidDate(String s) {        try {            dateFormat.parse(s);            return true;        } catch(Exception e) {            return false;        }    }    public static String formatDate(Date d) {        return dateFormat.format(d);    }}

DateFormat是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化和分析日期或时间。

更多java知识请关注java基础教程。

文章来源:智云一二三科技

文章标题:java判断字符串是否是日期

文章地址:https://www.zhihuclub.com/21489.shtml

关于作者: 智云科技

热门文章

网站地图