java時間和字符串之間相互轉(zhuǎn)換幾種方法
1.當前時間對象轉(zhuǎn)字符串
方法一:Date和SimpleDateFormat實現(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.時間字符串轉(zhuǎn)時間對象
方式一: 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:嚴格模式,日期、時間必須完全正確。
- SMART:智能模式,針對日可以自動調(diào)整。月的范圍在 1 到 12,日的范圍在 1 到 31。比如輸入是 2 月 30 號,當年 2 月只有 28 天,返回的日期就是 2 月 28 日。默認模式
- LENIENT:寬松模式,主要針對月和日,會自動后延。結(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)換后時間:" + 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)換后時間:" + localDateTime); } catch (Exception ex) { System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage()); } }
STRICT嚴格模式:
嚴格模式下時間字符串的年份格式化分為yyyy和uuuu兩種。當DateTimeFormatter.ofPattern()進行非嚴格模式下的格式化的時候,yyyy/MM/dd和uuuu/MM/dd表現(xiàn)相同,都是轉(zhuǎn)換為合法的日期。
yyyy:
yyyy
代表公元紀年,在嚴格模式下,如果時間字符串不包含公元,因此程序報錯。
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)換后時間:" + localDateTime); } catch (Exception ex) { System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage()); } }
使用G yyyy/MM/dd
來進行格式化 ,并且在時間字符串中也增加了公元單位。
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)換后時間:" + localDateTime); } catch (Exception ex) { System.err.println("轉(zhuǎn)換失敗:" + ex.getMessage()); } }
uuuu:
uuuu不代表公元格式化,所以不會存在異常。
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)換后時間:" + localDateTime); } catch (Exception ex) { System.err.println("轉(zhuǎn)換失敗:" + ex); } }
總結(jié)
到此這篇關(guān)于java時間和字符串之間相互轉(zhuǎn)換幾種方法的文章就介紹到這了,更多相關(guān)java時間和字符串轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA修改內(nèi)存大小,使得idea運行更流暢
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA修改內(nèi)存大小,使得idea運行更流暢的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10java并發(fā)編程專題(四)----淺談(JUC)Lock鎖
這篇文章主要介紹了java并發(fā)編程(JUC)Lock鎖的相關(guān)內(nèi)容,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06Java向kettle8.0傳遞參數(shù)的方式總結(jié)
介紹了如何在Kettle中傳遞參數(shù)到轉(zhuǎn)換和作業(yè)中,包括設(shè)置全局properties、使用TransMeta和JobMeta的parameterValue,以及通過EL表達式獲取參數(shù)值2025-01-01java selenium教程環(huán)境搭建基于Maven
本文主要介紹Java selenium 環(huán)境的安裝,這里介紹了基于Maven的環(huán)境搭建,有需要的小伙伴可以參考下2016-08-08