Determine whether a string like "2018-02-30" is a correct and reasonable date: (Recommended: java video tutorial)
//假设传入的日期格式是yyyy-MM-dd HH:mm:ss, 也可以传入yyyy-MM-dd,如2018-1-1或者2018-01-01格式 public static boolean isValidDate(String strDate) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2018-02-29会被接受,并转换成2018-03-01 format.setLenient(false); Date date = format.parse(strDate); //判断传入的yyyy年-MM月-dd日 字符串是否为数字 String[] sArray = strDate.split("-"); for (String s : sArray) { boolean isNum = s.matches("[0-9]+"); //+表示1个或多个(如"3"或"225"),*表示0个或多个([0-9]*)(如""或"1"或"22"),?表示0个或1个([0-9]?)(如""或"7") if (!isNum) { return false; } } } catch (Exception e) { // e.printStackTrace(); // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对 return false; } return true; }
SimpleDateFormat class
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-dependent manner, which allows formatting (date → text), parsing ( text → date) and normalization. SimpleDateFormat enables the selection of any user-defined date/time format pattern.
The SimpleDateFormat class mainly has the following three construction methods.
SimpleDateFormat(): Constructs SimpleDateFormat with the default format and default locale.
SimpleDateFormat(String pattern): Constructs SimpleDateF ormat with the specified format and default locale.
SimpleDateFormat(String pattern,Locale locale): Constructs SimpleDateF ormat with the specified format and the specified locale.
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of Java determines whether a string is a legal date. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft