您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何在Java中使用日期操作類,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
一 取出當(dāng)前日期時(shí)間
1 代碼
import java.time.*; public class GetDatetime { public static void main(String[] args) { // 創(chuàng)建時(shí)間對(duì)象,獲取當(dāng)前時(shí)間 LocalDateTime timePoint = LocalDateTime.now( ); // 當(dāng)前時(shí)間 System.out.println("--當(dāng)前時(shí)間----"); System.out.println(timePoint); System.out.println("--獲取時(shí)間的各個(gè)部分----"); System.out.println(timePoint.toLocalDate( )); System.out.println(timePoint.getMonth( )); System.out.println(timePoint.getDayOfMonth( )); System.out.println(timePoint.getSecond( )); } }
2 運(yùn)行
--當(dāng)前時(shí)間----
2019-07-06T08:10:31.458
--獲取時(shí)間的各個(gè)部分----
2019-07-06
JULY
6
31
二 將時(shí)間對(duì)象轉(zhuǎn)化為字符串
1 代碼
import java.time.*; import java.time.format.*; public class DateFormatDemo { public static void main(String[] args) { //獲取當(dāng)前時(shí)間 LocalDate locDate = LocalDate.now(); //指定格式化規(guī)則 DateTimeFormatter dateFmt1 = DateTimeFormatter.ofPattern("dd/MM/uuuu"); //將當(dāng)前時(shí)間格式化 String str1 = locDate.format(dateFmt1); System.out.println("時(shí)間格式1 : " + str1); DateTimeFormatter dateFmt2 = DateTimeFormatter.ofPattern("uuuu年MM月dd日"); //將當(dāng)前時(shí)間格式化 String str2 = locDate.format(dateFmt2); System.out.println("時(shí)間格式2 : " + str2); } }
2 運(yùn)行
時(shí)間格式1 : 06/07/2019
時(shí)間格式2 : 2019年07月06日
關(guān)于如何在Java中使用日期操作類就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。