Java實(shí)現(xiàn)時(shí)間和字符串互轉(zhuǎn)
1.當(dāng)前時(shí)間對(duì)象轉(zhuǎn)字符串
方法一:Date和SimpleDateFormat實(shí)現(xiàn)
使用java.util.Date類和java.text.SimpleDateFormat類
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(now);
System.out.println(currentTime);
}
方法二:LocalDateTime和DateTimeFormatter
使用java.time.LocalDateTime類和java.time.format.DateTimeFormatter類(Java 8及以上版本)
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String currentTime = now.format(dtf);
System.out.println(currentTime);
}
方式三:Calendar
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
String currentTime = String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
System.out.println(currentTime);
}
方式四:LocalDate和LocalTime
使用java.time.LocalDate類和java.time.LocalTime類(Java 8及以上版本)
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = currentDate.format(dtf);
String formattedTime = currentTime.format(dtf);
System.out.println(formattedDate + " " + formattedTime);
}
2.時(shí)間字符串轉(zhuǎn)時(shí)間對(duì)象
方式一: SimpleDateFormat
String dateString = "2024-10-28 22:56:11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.parse(dateString);
方式二:DateTimeFormatter
DateTimeFormatter有三種解析模式:
STRICT:嚴(yán)格模式,日期、時(shí)間必須完全正確。
SMART:智能模式,針對(duì)日可以自動(dòng)調(diào)整。月的范圍在 1 到 12,日的范圍在 1 到 31。比如輸入是 2 月 30 號(hào),當(dāng)年 2 月只有 28 天,返回的日期就是 2 月 28 日。默認(rèn)模式
LENIENT:寬松模式,主要針對(duì)月和日,會(huì)自動(dòng)后延。結(jié)果類似于LocalData#plusDays或者LocalDate#plusMonths。
SMART智能模式:
testTime("2023-02-33 22:56:11");
testTime("2023-02-30 22:56:11");
private static void testTime(String dateString){
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
try {
LocalDateTime localDateTime = LocalDateTime.parse(dateString, sdf);
System.out.println("轉(zhuǎn)換后時(shí)間:" + localDateTime);
} catch (Exception ex) {
System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage());
}
}
LENIENT寬松模式:
testTime("2023-02-33 22:56:11");
testTime("2023-02-30 22:56:11");
private static void testTime(String dateString){
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withResolverStyle(ResolverStyle.LENIENT);
try {
LocalDateTime localDateTime = LocalDateTime.parse(dateString, sdf);
System.out.println("轉(zhuǎn)換后時(shí)間:" + localDateTime);
} catch (Exception ex) {
System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage());
}
}
STRICT嚴(yán)格模式:
嚴(yán)格模式下時(shí)間字符串的年份格式化分為yyyy和uuuu兩種。當(dāng)DateTimeFormatter.ofPattern()進(jìn)行非嚴(yán)格模式下的格式化的時(shí)候,yyyy/MM/dd和uuuu/MM/dd表現(xiàn)相同,都是轉(zhuǎn)換為合法的日期。
yyyy:
yyyy代表公元紀(jì)年,在嚴(yán)格模式下,如果時(shí)間字符串不包含公元,因此程序報(bào)錯(cuò)。
testTime("2023-02-20 22:56:11");
testTime("2023-02-30 22:56:11");
private static void testTime(String dateString){
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withResolverStyle(ResolverStyle.STRICT);
try {
LocalDateTime localDateTime = LocalDateTime.parse(dateString, sdf);
System.out.println("轉(zhuǎn)換后時(shí)間:" + localDateTime);
} catch (Exception ex) {
System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage());
}
}
使用G yyyy/MM/dd來(lái)進(jìn)行格式化 ,并且在時(shí)間字符串中也增加了公元單位。
testTime("公元 2023-02-20 22:56:11");
testTime("公元 2023-02-30 22:56:11");
private static void testTime(String dateString){
/**
* Locale.JAPAN --> "西暦 yyyy-MM-dd"
* Locale.CHINA --> "公元 yyyy-MM-dd"
* Locale.US --> "AD yyyy-MM-dd"
*/
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("G yyyy-MM-dd HH:mm:ss",Locale.CHINESE)
.withResolverStyle(ResolverStyle.STRICT);
try {
LocalDateTime localDateTime = LocalDateTime.parse(dateString, sdf);
System.out.println("轉(zhuǎn)換后時(shí)間:" + localDateTime);
} catch (Exception ex) {
System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage());
}
}
uuuu:
uuuu不代表公元格式化,所以不會(huì)存在異常。
testTime("2023-02-20 22:56:11");
testTime("2023-02-29 22:56:11");
private static void testTime(String dateString){
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")
.withResolverStyle(ResolverStyle.STRICT);
try {
LocalDateTime localDateTime = LocalDateTime.parse(dateString, sdf);
System.out.println("轉(zhuǎn)換后時(shí)間:" + localDateTime);
} catch (Exception ex) {
System.err.println("轉(zhuǎn)換失敗:" + ex);
}
}
到此這篇關(guān)于Java實(shí)現(xiàn)時(shí)間和字符串互轉(zhuǎn)的文章就介紹到這了,更多相關(guān)Java時(shí)間和字符串互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
8個(gè)簡(jiǎn)單部分開啟Java語(yǔ)言學(xué)習(xí)之路 附j(luò)ava學(xué)習(xí)書單
8個(gè)簡(jiǎn)單部分開啟Java語(yǔ)言學(xué)習(xí)之路,附j(luò)ava學(xué)習(xí)書單,這篇文章主要向大家介紹了學(xué)習(xí)java語(yǔ)言的方向,感興趣的小伙伴們可以參考一下2016-09-09
關(guān)于SpringBoot禁止循環(huán)依賴解說(shuō)
這篇文章主要介紹了關(guān)于SpringBoot禁止循環(huán)依賴解說(shuō),Spring的Bean管理,文章圍繞主題展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05
springboot+vue+elementsUI實(shí)現(xiàn)分角色注冊(cè)登錄界面功能
這篇文章主要給大家介紹了關(guān)于springboot+vue+elementsUI實(shí)現(xiàn)分角色注冊(cè)登錄界面功能的相關(guān)資料,Spring?Boot和Vue.js是兩個(gè)非常流行的開源框架,可以用來(lái)構(gòu)建Web應(yīng)用程序,需要的朋友可以參考下2023-07-07
如何理解Java中基類子對(duì)象的構(gòu)建過(guò)程從"基類向外"進(jìn)行擴(kuò)散的?
今天小編就為大家分享一篇關(guān)于如何理解Java中基類子對(duì)象的構(gòu)建過(guò)程從"基類向外"進(jìn)行擴(kuò)散的?,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
解決SpringBoot多模塊發(fā)布時(shí)99%的問(wèn)題
本文歸納了以下 8 個(gè)原則和發(fā)布時(shí)經(jīng)常出現(xiàn)的 4 個(gè)問(wèn)題的解決方案,掌握了這些原則和解決方案,幾乎可以解決絕大數(shù)SpringBoot發(fā)布問(wèn)題2019-07-07
spring boot補(bǔ)習(xí)系列之幾種scope詳解
這篇文章主要給大家介紹了關(guān)于spring boot補(bǔ)習(xí)系列之幾種scope的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Java子類對(duì)象的實(shí)例化過(guò)程分析
這篇文章主要介紹了Java子類對(duì)象的實(shí)例化過(guò)程,結(jié)合具體實(shí)例形式分析了java子類對(duì)象的實(shí)例化的步驟、原理、實(shí)現(xiàn)方法,需要的朋友可以參考下2019-09-09

