我有日期格式在2016年2月20日我必須將其轉換爲2016年2月26日請建議我如何將其轉換爲此格式。如何將日期轉換爲java中的2016年2月26日
回答
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
String theInDate = "2/20/2016";
String theInFormat = "MM/dd/yyyy";
String theOutFormat = "MMM dd, yyyy";
final SimpleDateFormat theSdfInputFormatter = new SimpleDateFormat(theInFormat);
final SimpleDateFormat theSdfOutputFormatter = new SimpleDateFormat(theOutFormat);
final Date theDate = theSdfInputFormatter.parse(theInDate);
final String theDateText = theSdfOutputFormatter.format(theDate);
System.out.println(theDateText);
}
}
您可能還想檢查日期解析器中的setLinient(true)功能。
,並用新的日期API
String theInDate = "2/20/2016";
String theInFormat = "M/d/yyyy";
String theOutFormat = "MMM dd, yyyy";
final DateTimeFormatter theSdfInputFormatter = DateTimeFormatter.ofPattern(theInFormat);
final DateTimeFormatter theSdfOutputFormatter = DateTimeFormatter.ofPattern(theOutFormat);
final LocalDate theDate = LocalDate.from(theSdfInputFormatter.parse(theInDate));
final String theDateText = theSdfOutputFormatter.format(theDate);
System.out.println(theDateText);
注意線程安全的。看看javadoc。 此解決方案適用於Java。對於Android,標記爲,應該非常相似。 即使這是一個重複的答案,我希望它可以幫助某人快速解決問題,並理解它如何用於進一步自我學習。
回答明顯的重複問題沒有幫助。 –
僅供參考,麻煩的舊日期時間類如['java.util.Date'](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html),[ 'java.util.Calendar'](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html)和'java.text.SimpleTextFormat'現在是[legacy]( https://en.wikipedia.org/wiki/Legacy_system),由[java.time]代替(https://docs.oracle.com/javase/8/docs/api/java/time/package-summary。 html)類。請參見[Oracle教程](https://docs.oracle.com/javase/tutorial/datetime/TOC.html)。 –
- 1. 如何將日期值「2017年5月26日」轉換爲「26/05/2017」?
- 2. Preg match以這種格式查找所有發生的日期26 2016年1月26日2016年1月26日或2016年1月26日
- 3. 如何將給定日期轉換爲java,年,月,日
- 4. 如何將日期轉換爲年,月,日日期格式
- 5. 如何字符串日期2015年2月12日轉換爲2015年12月2日在java中
- 6. Excel單元格將2016年8月轉換爲日期
- 7. C# - 如何轉換26月/ 6/2016在13:14至2016年1月26日13時14分零零秒
- 8. 如何在Excel表格中將日/月/年的日期轉換爲月/日/年的日期
- 9. 如何將日期(02/07/2012)轉換爲2012年2月7日星期二
- 10. 從任何日期格式將日期轉換爲完整日期格式(2016年7月27日星期二)
- 11. 如何將日期時間轉換爲日/月/日/月/年yyyy格式
- 12. 如何解析日期字符串形式爲「2016年11月17日7:26:57 PM」的日期?
- 13. java如何將FormatStyle.MEDIUM(「2016年11月30日」)字符串轉換爲LocalDateTime類型?
- 14. 如何將蜂巢日期轉換爲以下格式:從2016-11-28到2016年11月28日?
- 15. 如何轉換毫秒自1970年1月1日00:00:00至日期2016年1月20日
- 16. 轉換日期(年月日)到天數
- 17. 將以秒爲單位的日期轉換爲「日月年」日期
- 18. 如何將「2015年1月30日」轉換爲R中的日期對象?
- 19. 將日期時間轉換爲月,日和年?
- 20. 將日期從1901年1月01日轉換爲unix/linux
- 21. 將2012年8月7日轉換爲php日期格式
- 22. 將MM/DD/YYYY日期轉換爲月日年
- 23. 將「2012年3月2日」轉換爲日期時間對象的Python
- 24. 如何將日/月/年日期轉換爲自0001年1月1日以來.NET樣式100ns的勾號?
- 25. 我如何將日期轉換爲日/月/年(20/Jan/2012)在python中
- 26. 如何將整數日期轉換爲格式化日期字符串(即2012009至2009年2月1日)
- 27. 日期的Java日期格式字符串「2013年1月2日星期三下午5:29:26 +02:00」
- 28. C++如何將年 - 月 - 日格式的日期轉換爲unix時代格式?
- 29. 月份和年份的日期轉換
- 30. 將年份日月轉換爲月份日
退房文檔:https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – Confiqure
ü可以建議我,我必須採取 –
它的文檔是什麼哪種格式 – Confiqure