Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Java verify that the time format is correct?

Shulou Source: shulou.com Published: 2022-05-31 22:41:41 10月01日 Update

This article mainly introduces Java how to verify whether the time format is correct related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Java how to verify whether the time format is correct the article will have a harvest, let's take a look.

In many scenarios, we need to verify that the time and date is in the correct format and that the time is regular.

1. Verify the date in yyyy-MM-dd HH:mm:dd format

String date = "2020-01-25 12:36:45"; System.out.println ("date" + isLegalDate (date.length (), date, "yyyy-MM-dd HH:mm:ss"))

2. Verify the date in yyyy-MM-dd format

String yearMonthday = "2020-01-01"; System.out.println ("yearMonthday:" + isLegalDate (yearMonthday.length (), yearMonthday, "yyyy-MM-dd"))

3. Verify the date in yyyy-MM format

String yearMonth = "2020-02"; System.out.println ("yearMonth:" + isLegalDate (yearMonth.length (), yearMonth, "yyyy-MM"))

4. Verify the date in yyyy format

String year = "2020"; System.out.println ("year:" + isLegalDate (year.length (), year, "yyyy"))

5. Verify the date in HH:mm:ss format

String hms = "12:36:89"; System.out.println ("hms:" + isLegalDate (hms.length (), hms, "HH:mm:ss"))

6. Here is a complete method class that can be run directly to verify whether the date format is correct.

Package com.shucha.deveiface.biz.test; import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date / * * @ author tqf * @ Description time format check * @ Version 1.0 * @ since 2020-09-15 16:49 * / public class IsLegalDate {public static void main (String [] args) {/ / 1, date of verification yyyy-MM-dd HH:mm:dd format String date = "2020-01-25 12:36:45" System.out.println ("date" + isLegalDate (date.length (), date, "yyyy-MM-dd HH:mm:ss"); / 2. Verify the date in yyyy-MM-dd format String yearMonthday = "2020-01-01"; System.out.println ("yearMonthday:" + isLegalDate (yearMonthday.length (), yearMonthday, "yyyy-MM-dd")) / / 3. Verify the date in yyyy-MM format String yearMonth = "2020-02"; System.out.println ("yearMonth:" + isLegalDate (yearMonth.length (), yearMonth, "yyyy-MM")); / / 4. Verify the date in yyyy format String year = "2020"; System.out.println ("year:" + isLegalDate (year.length (), year, "yyyy")) } / * check whether it is correct according to time and time format * @ length of param length check * @ date of param sDate check * @ format of param format check * @ return * / public static boolean isLegalDate (int length, String sDate,String format) {int legalLen = length If ((sDate = = null) | | (sDate.length ()! = legalLen)) {return false;} DateFormat formatter = new SimpleDateFormat (format); try {Date date = formatter.parse (sDate); return sDate.equals (formatter.format (date));} catch (Exception e) {return false;}

The following is a screenshot after time verification

This is the end of the article on "how to verify whether the time format is correct by Java". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to verify whether the time format is correct by Java". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Format validation date time knowledge content article value scene general screenshot easy to operate article method easy to understand more look industry information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi NVidia Apple Linux