Java日期時間與正則表達式超詳細整理(適合新手入門)
1、java.time.LocalDate類表示日期;
你可以使用該類的now()方法獲取當(dāng)前日期,或者使用of()方法創(chuàng)建一個指定日期的實例,例如:
LocalDate today = LocalDate.now(); LocalDate myBirthday = LocalDate.of(2000, Month.JANUARY, 1);
2、java.time.LocalTime類表示時間;
你可以使用該類的now()方法獲取當(dāng)前時間,或者使用of()方法創(chuàng)建一個指定時間的實例,例如:
LocalTime now = LocalTime.now(); LocalTime sixThirty = LocalTime.of(6, 30);
3、java.time.LocalDateTime類表示日期和時間;
你可以使用該類的now()方法獲取當(dāng)前日期和時間,或者使用of()方法創(chuàng)建一個指定日期和時間的實例,例如:
LocalDateTime now = LocalDateTime.now(); LocalDateTime dateTime = LocalDateTime.of(2023, Month.JANUARY, 1, 6, 30);
4、java.time.format.DateTimeFormatter類用于格式化日期和時間;
你可以使用該類的ofPattern()方法創(chuàng)建一個格式化模板,然后使用該模板的format()方法格式化日期和時間,例如:
LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = now.format(formatter);
5、創(chuàng)建正則表達式對象
可以使用java.util.regex.Pattern類創(chuàng)建正則表達式對象,該類提供了compile()方法來編譯正則表達式,并返回一個Pattern對象。例如:
Pattern pattern = Pattern.compile("regex");
6、匹配字符串
可以使用Matcher類來匹配字符串,并執(zhí)行相應(yīng)的操作,例如:可以使用matches()方法來檢查給定的字符串是否與正則表達式匹配
String input = "example string"; Pattern pattern = Pattern.compile("example.*"); Matcher matcher = pattern.matcher(input); if (matcher.matches()) { System.out.println("Match found"); }
7、查找匹配
可以使用find()方法在給定的輸入中查找正則表達式的匹配項。例如:
String input = "example string"; Pattern pattern = Pattern.compile("example.*"); Matcher matcher = pattern.matcher(input); if (matcher.find()) { System.out.println("Match found at index " + matcher.start()); }
8、替換字符串
可以使用replaceAll()方法來替換匹配正則表達式的字符串。例如:
String input = "example string"; Pattern pattern = Pattern.compile("example"); Matcher matcher = pattern.matcher(input); String output = matcher.replaceAll("replacement"); System.out.println(output);
9、匹配模式
Java中的正則表達式支持許多模式,可以使用這些模式來調(diào)整匹配行為。例如,可以使用Pattern.CASE_INSENSITIVE模式來進行不區(qū)分大小寫的匹配:
String input = "Example String"; Pattern pattern = Pattern.compile("example.*", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(input); if (matcher.matches()) { System.out.println("Match found"); }
附:java日期正則表達式
// 日期各種格式y(tǒng)yyy-MM-dd HH:mm:ss、 yyyy-MM-dd等 String dateRgx = "(([1-3][0-9]{3})[-]{0,1}(((0[13578]|1[02])[-]{0,1}(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)[-]{0,1}(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))\\s\\d{1,2}:\\d{1,2}:\\d{1,2})|(([1-3][0-9]{3})[-]{0,1}(((0[13578]|1[02])[-]{0,1}(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))\\s\\d{1,2}:\\d{1,2})|(([1-3][0-9]{3})[-]{0,1}(((0[13578]|1[02])[-]{0,1}(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)[-]{0,1}(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))"; String date1 = "2019-08-28"; String date2 = "abc"; System.out.println(date1.matches(dateRgx));//true System.out.println(date2.matches(dateRgx));//false
總結(jié)
到此這篇關(guān)于Java日期時間與正則表達式(適合新手入門)的文章就介紹到這了,更多相關(guān)Java日期時間與正則表達式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一
對比原生Mybatis, Mybatis Plus或者其他框架,F(xiàn)luentMybatis提供了哪些便利呢?很多朋友對這一問題不是很清楚,今天小編給大家?guī)硪黄坛剃P(guān)于Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一,一起看看吧2021-08-08springboot整合JSR303校驗功能實現(xiàn)代碼
這篇文章主要介紹了springboot整合JSR303校驗功能實現(xiàn),JSR303校驗方法有統(tǒng)一校驗的需求,統(tǒng)一校驗實現(xiàn)以及分組校驗,本文結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-01-01解決springboot的aop切面不起作用問題(失效的排查)
這篇文章主要介紹了解決springboot的aop切面不起作用問題(失效的排查),具有很好的參考價值,希望對大家有所幫助。 一起跟隨小編過來看看吧2020-04-04mybatis修改int型數(shù)據(jù)無法修改成0的解決
這篇文章主要介紹了mybatis修改int型數(shù)據(jù)無法修改成0的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09關(guān)于JVM默認(rèn)堆內(nèi)存大小問題
這篇文章主要介紹了關(guān)于JVM默認(rèn)堆內(nèi)存大小問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02部署springboot項目到云服務(wù)器的兩種方式(jar+war)
本文主要介紹了部署springboot項目到云服務(wù)器的兩種方式,主要介紹了jar和war兩種方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12