我有日期格式在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. 如何在iOS中將日期「2012-12-26」轉換爲「2012年12月26日」?
- 2. Preg match以這種格式查找所有發生的日期26 2016年1月26日2016年1月26日或2016年1月26日
- 3. 如何將日期值「2017年5月26日」轉換爲「26/05/2017」?
- 4. 如何將給定日期轉換爲java,年,月,日
- 5. 如何將日期轉換爲年,月,日日期格式
- 6. 如何字符串日期2015年2月12日轉換爲2015年12月2日在java中
- 7. C#將日期時間轉換爲2014年7月26日星期四格式
- 8. Excel單元格將2016年8月轉換爲日期
- 9. C# - 如何轉換26月/ 6/2016在13:14至2016年1月26日13時14分零零秒
- 10. 如何在Excel表格中將日/月/年的日期轉換爲月/日/年的日期
- 11. 將日期轉換爲年和月
- 12. 從任何日期格式將日期轉換爲完整日期格式(2016年7月27日星期二)
- 13. 如何將日期(02/07/2012)轉換爲2012年2月7日星期二
- 14. 如何將2016年1月12日像javascript中的日期字符串轉換爲2016-01-12 00:00:00
- 15. 如何將這種日期轉換爲月份日年格式?
- 16. 如何將號碼日期轉換爲年,月,日?
- 17. 如何將日期時間轉換爲日/月/日/月/年yyyy格式
- 18. 將特定字符串(0716_F_Frndy)轉換爲日期(2016年7月1日)
- 19. PHP:將年中的日期轉換爲月份和月中的日期
- 20. 如何解析日期字符串形式爲「2016年11月17日7:26:57 PM」的日期?
- 21. java如何將FormatStyle.MEDIUM(「2016年11月30日」)字符串轉換爲LocalDateTime類型?
- 22. 如何將蜂巢日期轉換爲以下格式:從2016-11-28到2016年11月28日?
- 23. 如何轉換毫秒自1970年1月1日00:00:00至日期2016年1月20日
- 24. 將「2013年2月1日星期五」轉換爲「2013-02-01」
- 25. 轉換日期(年月日)到天數
- 26. 將以秒爲單位的日期轉換爲「日月年」日期
- 27. MYSQL如何轉換「2013年8月21日」爲日期格式
- 28. 如何將「2015年1月30日」轉換爲R中的日期對象?
- 29. 將MM/DD/YYYY日期轉換爲月日年
- 30. 將日期從1901年1月01日轉換爲unix/linux
退房文檔:https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – Confiqure
ü可以建議我,我必須採取 –
它的文檔是什麼哪種格式 – Confiqure