Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java verify that the time format is correct?

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report