Java驗證時間格式是否正確方法類項目實戰(zhàn)
在很多場景中我們需要驗證時間日期的是否屬于正確的格式,驗證時間是否符合常規(guī)的。
1、驗證 yyyy-MM-dd HH:mm:dd 格式的日期
String date = "2020-01-25 12:36:45"; System.out.println("date "+isLegalDate(date.length(),date,"yyyy-MM-dd HH:mm:ss"));
2、驗證 yyyy-MM-dd 格式的日期
String yearMonthday = "2020-01-01"; System.out.println("yearMonthday: "+isLegalDate(yearMonthday.length(),yearMonthday,"yyyy-MM-dd"));
3、驗證 yyyy-MM 格式的日期
String yearMonth = "2020-02"; System.out.println("yearMonth: "+isLegalDate(yearMonth.length(),yearMonth,"yyyy-MM"));
4、驗證 yyyy 格式的日期
String year = "2020"; System.out.println("year: "+isLegalDate(year.length(),year,"yyyy"));
5、驗證 HH:mm:ss 格式的日期
String hms = "12:36:89"; System.out.println("hms: "+isLegalDate(hms.length(),hms,"HH:mm:ss"));
6、下面是一個完整的方法類直接運行就可以實現(xiàn)驗證日期格式是否正確的
package com.shucha.deveiface.biz.test; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** * @author tqf * @Description 時間格式校驗 * @Version 1.0 * @since 2020-09-15 16:49 */ public class IsLegalDate { public static void main(String[] args) { //1、驗證 yyyy-MM-dd HH:mm:dd 格式的日期 String date = "2020-01-25 12:36:45"; System.out.println("date "+isLegalDate(date.length(),date,"yyyy-MM-dd HH:mm:ss")); //2、驗證 yyyy-MM-dd 格式的日期 String yearMonthday = "2020-01-01"; System.out.println("yearMonthday: "+isLegalDate(yearMonthday.length(),yearMonthday,"yyyy-MM-dd")); //3、驗證 yyyy-MM 格式的日期 String yearMonth = "2020-02"; System.out.println("yearMonth: "+isLegalDate(yearMonth.length(),yearMonth,"yyyy-MM")); //4、驗證 yyyy 格式的日期 String year = "2020"; System.out.println("year: "+isLegalDate(year.length(),year,"yyyy")); //5、驗證 HH:mm:ss 格式的日期 String hms = "12:36:89"; System.out.println("hms: "+isLegalDate(hms.length(),hms,"HH:mm:ss")); } /** * 根據(jù)時間 和時間格式 校驗是否正確 * @param length 校驗的長度 * @param sDate 校驗的日期 * @param format 校驗的格式 * @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; } } }
下面是一個時間驗證之后的截圖
到此這篇關(guān)于Java驗證時間格式是否正確方法類項目實戰(zhàn)的文章就介紹到這了,更多相關(guān)Java驗證時間格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java啟動參數(shù)(-,?-X,?-XX參數(shù))的使用
本文主要介紹了Java啟動參數(shù)(-,?-X,?-XX參數(shù))的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06如何利用Java?AWT?創(chuàng)建一個簡易計算器
這篇文章主要介紹了如何利用Java?AWT?創(chuàng)建一個簡易計算器,AWT?是一個有助于構(gòu)建?GUI?的?API?基于?java?應(yīng)用程序,下面關(guān)于其相關(guān)資料實現(xiàn)計算器的內(nèi)容詳細(xì),需要的朋友可以參考一下2022-03-03MyBatis使用resultMap如何解決列名和屬性名不一致
這篇文章主要介紹了MyBatis使用resultMap如何解決列名和屬性名不一致的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Java HtmlEmail 郵件發(fā)送的簡單實現(xiàn)代碼
下面小編就為大家?guī)硪黄狫ava HtmlEmail 郵件發(fā)送的簡單實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06Java實現(xiàn)斷點下載服務(wù)端與客戶端的示例代碼
這篇文章主要為大家介紹了如何實現(xiàn)服務(wù)端(Spring Boot)與客戶端(Android)的斷點下載與下載續(xù)傳功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-08-08