修改date格式 java_如何用Java更改日期格式?
LocalDate.parse(
"23/01/2017"?,
DateTimeFormatter.ofPattern(?"dd/MM/uuuu"?,?Locale.UK?)?).format(
DateTimeFormatter.ofPattern(?"uuuu/MM/dd"?,?Locale.UK?))2017/01/23
避免遺留日期-時(shí)間類
這個(gè)克里斯托弗·帕克的答復(fù)是正確但過時(shí)的。麻煩的舊日期時(shí)間類,如java.util.Date,?java.util.Calendar,和java.text.SimpleTextFormat現(xiàn)在遺產(chǎn),被java.time上課。
使用java.time
將輸入字符串解析為日期時(shí)間對(duì)象,然后以所需格式生成新的字符串對(duì)象。
這個(gè)LocalDate類表示一個(gè)日期純值,沒有一天的時(shí)間,也沒有時(shí)區(qū).DateTimeFormatter?fIn?=?DateTimeFormatter.ofPattern(?"dd/MM/uuuu"?,?Locale.UK?);??//?As?a?habit,?specify?the?desired/expected?locale,?though?in?this?case?the?locale?is?irrelevant.LocalDate?ld?=?LocalDate.parse(?"23/01/2017"?,?fIn?);
為輸出定義另一個(gè)格式化程序。DateTimeFormatter?fOut?=?DateTimeFormatter.ofPattern(?"uuuu/MM/dd"?,?Locale.UK?);String?output?=?ld.format(?fOut?);2017/01/23
順便說一下,考慮使用標(biāo)準(zhǔn)ISO 8601表示日期時(shí)間值的字符串的格式。
關(guān)于java.time
若要了解更多信息,請(qǐng)參見Oracle教程..并搜索堆棧溢出的許多例子和解釋。規(guī)格是JSR 310.
在哪里獲得java.time類?內(nèi)置的。
帶有捆綁實(shí)現(xiàn)的標(biāo)準(zhǔn)JavaAPI的一部分。
Java 9添加了一些次要的特性和修復(fù)。中的大部分java.time功能都被移植到Java 6&7中。這個(gè)項(xiàng)目適應(yīng)
Threeten-Backport(上面提到)專門用于Android。
這個(gè)三次-額外項(xiàng)目使用其他類擴(kuò)展java.time。這個(gè)項(xiàng)目是java.time將來可能添加的一個(gè)試驗(yàn)場(chǎng)。您可能會(huì)在這里發(fā)現(xiàn)一些有用的類,例如Interval,?YearWeek,?YearQuarter,和更多.
尤達(dá)-時(shí)間
最新情況:這個(gè)尤達(dá)-時(shí)間項(xiàng)目現(xiàn)在在維護(hù)模式,小組建議遷移到j(luò)ava.time上課。這里的這一節(jié)是為了歷史而留下的。
為了好玩,下面是他的代碼,用于使用尤達(dá)-時(shí)間圖書館。//???2013?Basil?Bourque.?This?source?code?may?be?used?freely?forever?by?anyone?taking?full?responsibility?for?doing?so.//?import?org.joda.time.*;//?import?org.joda.time.format.*;final?String?OLD_FORMAT?=?"dd/MM/yyyy";final?String?NEW_FORMAT?=?"yyyy/MM/dd";//?August?12,?2010String?oldDateString?=?"12/08/2010";String?newDateString;DateTimeFormatter?formatterOld?=?DateTimeFormat.forPattern(OLD_FORMAT);DateTimeFormatter?formatterNew?=?DateTimeFormat.forPattern(NEW_FORMAT);LocalDate?localDate?=?formatterOld.parseLocalDate(?oldDateString?);newDateString?=?formatterNew.print(?localDate?);
轉(zhuǎn)儲(chǔ)到控制臺(tái)…System.out.println(?"localDate:?"?+?localDate?);System.out.println(?"newDateString:?"?+?newDateString?);
運(yùn)行…時(shí)localDate:?2010-08-12
newDateString:?2010/08/12
總結(jié)
以上是生活随笔為你收集整理的修改date格式 java_如何用Java更改日期格式?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022年汽车销量最高的城市有哪些?速看
- 下一篇: java编程最新图书_清华大学出版社-图