-1
A
回答
5
我添加的代碼制定出正確的後綴,因爲這不是標準的JDK的一部分。公平地說,這可能是這個問題的唯一不只是SimpleDateFormat#format()
電話。
static String[] suffixes =
// 0 1 2 3 4 5 6 7 8 9
{ "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
// 10 11 12 13 14 15 16 17 18 19
"th", "th", "th", "th", "th", "th", "th", "th", "th", "th",
// 20 21 22 23 24 25 26 27 28 29
"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
// 30 31
"th", "st" };
public static void main(String args[]) throws ParseException {
String string = "2013-01-10 09:49:19";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(string);
SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
SimpleDateFormat monthFormat = new SimpleDateFormat("MMM");
String dayStr =
dayFormat.format(date)
+ suffixes[Integer.parseInt(dayFormat.format(date))]
+ " " + monthFormat.format(date) + ".";
System.out.println(dayStr);
}
+0
+1 forawesome答案。 –
1
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-01-10 09:49:19");
String format = new SimpleDateFormat("dd'th' MMM").format(date);
System.out.println(format);
輸出是:
10th Jan
+0
這是不正確的,好像它是在本月3日,它會說1月3日,這是不正確的。 – david99world
0
使用DateFormat類
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ENGLISH);
System.out.println(df.parse(myDateInString));
我想你應該在周圍嘗試捕捉可能的異常的語句。
相關問題
- 1. 轉換日期從一種格式轉換爲另一種
- 2. 轉換日期爲另一種格式
- 3. 如何日期從一種格式轉換爲另一種golang
- 4. 使用PHP從一種日期格式轉換爲另一種
- 5. 在java中將日期從一種格式轉換爲另一種格式
- 6. 轉換的UUID從一種格式轉換爲另一種
- 7. 如何將一種日期格式轉換爲另一種日期格式
- 8. 從一種日期樣式轉換爲另一種
- 9. 將日期從一種格式轉換爲另一種格式 - AngularJS
- 10. 如何將日期從一種格式轉換爲另一種格式
- 11. 如何將日期從一種格式轉換爲另一種格式
- 12. 從一個日期格式轉換爲另一種在PHP
- 13. 將JSON從一種格式轉換爲另一種格式?
- 14. 將圖像從一種格式轉換爲另一種格式
- 15. 從一種格式轉換MySQL的日期到另一個
- 16. 如何在Java中從一種日期格式轉換爲另一種日期格式?
- 17. 使用Python將日期格式轉換爲另一種格式
- 18. 在Android中將日期格式轉換爲另一種格式?
- 19. 從一種格式轉換爲另日期不起作用
- 20. 如何日期從一種格式轉換爲另一種在XSLT
- 21. 如何在Java中將一種日期格式轉換爲另一種日期格式?
- 22. 將日期時間轉換爲C#中的另一種格式
- 23. 轉換日期爲另一種格式的PHP
- 24. 將日期格式轉換爲R中的另一種
- 25. 如何將mysql從一種日期格式轉換爲另一種日期格式
- 26. 將日期字符串對象轉換爲另一種格式
- 27. 如何在JavaScript中將日期轉換爲另一種格式?
- 28. 如何在Swift中將日期格式轉換爲另一種日期格式?
- 29. 將Excel電子表格從一種格式轉換爲另一種格式
- 30. 在java中將一種json格式轉換爲另一種格式
http://developer.android.com/reference/java/text/SimpleDateFormat.html – david99world
我無法想象你如何能夠*不*發現如何解析和格式化與Java的日期。 –