2015-10-28 85 views
1

我有一個字符串「星期六,十月,25日,11點40分」定義日期格式的Java「第三屆「ST」「日」「第二」

什麼格式做這個日期?我如何解析ordinal indicator

這裏是我怎麼想它

private String changeDateFormat(String stringDate){ 

     DateFormat dateFormat = new SimpleDateFormat("DD, MM, ddth, hh:mm"); 
     try { 
      Date date = dateFormat.parse(stringDate); 
      dateFormat = new SimpleDateFormat("ddMMMyyyy"); 
      stringDate=dateFormat.format(date); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     return stringDate.toUpperCase(); 
    } 
+1

您需要了解的有關'DateFormat'的所有信息 - http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Abubakkar

+0

如何定義「25th」?我的意思是「th」的東西 –

+0

@JenyaKyrmyza檢查出以前的SO問題 - http://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say -11th-21st-or-23rd-in-java –

回答

1

SimpleDateFormat似乎並不輕鬆處理日的數字後面加上「日」,「日」等簡單的解決辦法可以去除原始字符串的一部分。例如。

int comma = original.lastIndexOf(','); 
String stringDate = 
     original.substring(0, comma - 2) + 
     original.substring(comma + 1); 

之後只使用在stringDate這種格式:

SimpleDateFormat("EE, MM, dd, hh:mm") 
1

的Java文檔有很多關於如何解析從字符串的不同格式的日期信息轉換:

SimpleDateFormat Java Doc

你可以試試與此,但玩這個並參考Java文檔,直到你能夠解決你的問題:

DateFormat dateFormat = new SimpleDateFormat("E, MMM, dd'th' HH:mm"); 
Date date = dateFormat.parse("Saturday, Oct, 25th, 11:40"); 

嘗試不同的組合,這樣您將更多地瞭解SimpleDateFormat以及它如何與不同格式一起工作。

+0

如果會有第21或第22個呢? –

+0

即使25日也沒有工作 –

+0

你可以看看這個例子http://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-第11-21,23或in-java – Abubakkar

1

希望這個程序解決您的問題。

public class Test 
{ 
    public static void main(String[] args) { 
    System.out.println(new Test().getCurrentDateInSpecificFormat(Calendar.getInstance())); 
    } 

    private String getCurrentDateInSpecificFormat(Calendar currentCalDate) { 
    String dayNumberSuffix = getDayNumberSuffix(currentCalDate.get(Calendar.DAY_OF_MONTH)); 
    DateFormat dateFormat = new SimpleDateFormat("E, MMM, dd'"+ dayNumberSuffix +"', HH:mm"); 
    return dateFormat.format(currentCalDate.getTime()); 
    } 

    private String getDayNumberSuffix(int day) { 
    if (day >= 11 && day <= 13) { 
     return "th"; 
    } 
    switch (day % 10) { 
    case 1: 
     return "st"; 
    case 2: 
     return "nd"; 
    case 3: 
     return "rd"; 
    default: 
     return "th"; 
    } 
    } 

} 
+0

本地化怎麼辦? –

+0

@JenyaKyrmyza localiztions是指? –

+0

不同的語言 –

1

一個解決辦法是分析參數,以便知道使用哪種模式:

private String changeDateFormat(String stringDate) { 
    DateFormat dateFormat; 
    if (stringDate.matches("^([a-zA-Z]+,){2}[0-9]+st, [0-9]{2}:[0-9]+{2}")) { 
     dateFormat = new SimpleDateFormat("E, MMM, dd'st', HH:mm"); 
    } else if (stringDate.matches("^([a-zA-Z]+,){2}[0-9]+nd, [0-9]{2}:[0-9]+{2}")) { 
     dateFormat = new SimpleDateFormat("E, MMM, dd'nd', HH:mm"); 
    } else if (stringDate.matches("^([a-zA-Z]+,){2}[0-9]+rd, [0-9]{2}:[0-9]+{2}")) { 
     dateFormat = new SimpleDateFormat("E, MMM, dd'rd', HH:mm"); 
    } else { 
     dateFormat = new SimpleDateFormat("E, MMM, dd'th', HH:mm"); 
    } 
    try { 
     Date date = dateFormat.parse(stringDate); 
     dateFormat = new SimpleDateFormat("ddMMMyyyy"); 
     stringDate = dateFormat.format(date); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    return stringDate.toUpperCase(); 
} 

這可能需要一些優化,但它提供了這個想法。

相關問題