Java使用DateFormatter格式化日期時間的方法示例
本文實例講述了Java使用DateFormatter格式化日期時間的方法。分享給大家供大家參考,具體如下:
Java版本:1.8開始
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; /** * Created by Frank */ public class CurrentDatetime { public static void main(String[] args) { LocalDate dNow = LocalDate.now(); System.out.println(dNow); LocalTime tNow = LocalTime.now(); System.out.println(tNow); LocalDateTime now = LocalDateTime.now(); System.out.println(now); DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd"); System.out.println(df.format(LocalDateTime.now())); System.out.println(LocalDate.parse("2016/11/28", df)); DateTimeFormatter nTZ = DateTimeFormatter.ofPattern("d MMMM, yyyy h:mm a"); System.out.println(ZonedDateTime.now().format(nTZ)); /* 輸出: 2017-04-02 16:43:29.988 2017-04-02T16:43:29.988 2017/04/02 2016-11-28 2 四月, 2017 4:43 下午 */ } }
運行效果如下圖:
注:java.time為JDK1.8版本開始引入,另外本例測試環(huán)境使用的eclipse為4.4版本以上(本站下載地址:http://www.dbjr.com.cn/softs/390522.html),否則版本過低會提示錯誤!
PS:這里再為大家推薦幾款時間及日期相關(guān)工具供大家參考使用:
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計算器/相差天數(shù)計算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數(shù)差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Java commons-httpclient如果實現(xiàn)get及post請求
這篇文章主要介紹了Java commons-httpclient如果實現(xiàn)get及post請求,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09java.io.IOException:你的主機中的軟件中止了一個已建立的連接踩坑實戰(zhàn)
最近在工作中遇到了個問題,分享給同樣遇到問題的同學(xué),這篇文章主要給大家介紹了關(guān)于java.io.IOException:你的主機中的軟件中止了一個已建立的連接的踩坑實戰(zhàn)記錄,需要的朋友可以參考下2023-03-03