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推断字符串是不是是日期的细致内容,更多请关注ki4网别的相干文章!