2013-10-18 104 views
0

我正在開發需要將非英語日期轉換爲標準JAVA日曆/日期格式的項目。例如像helmikuu 13, 2013這樣的日期應該轉換爲2013年2月13日。將非英語日期轉換爲標準Java日期

我認爲它可以使用Java簡單日期格式定位函數。我試圖使用此代碼,但它會拋出Unparseable date: "helmikuu 13, 2013"錯誤。

public static void main(String args[]) throws Exception { 
     String dateInFin = "helmikuu 13, 2013"; 
     Locale localeFi = new Locale("fi", "FI"); 
     DateFormat dateFormatFin = new SimpleDateFormat("MMMMM dd, yyyy"); 
     dateFormatFin.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, localeFi); 

     System.out.println("Locale: "+localeFi.getDisplayName()); 
     System.out.println(dateFormatFin.parse(dateInFin)); 

    } 

回答

0

使用java.text.DateFormat

DateFormat format = new SimpleDateFormat("MMMM d, yyyy"); 

使用parse(String)format()方法分析和格式化日期。

+0

但我的日期字符串不是英文我怎麼能使用相同的功能。 – Alex

4

例如,你可以做這樣的(例子是如何從法語的語言環境轉換成Lihuanian):

@Test 
    public void testTestLocale() throws Exception { 
     Date date = new Date(); 

     // Get a France locale 
     Locale localeFR = Locale.FRANCE; 

     // Create a Lithuanian Locale 
     Locale localeLT = new Locale("lt", "LT"); 

     // Get a date time formatter for display in France 
     DateFormat fullDateFormatFR =DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,localeFR); 

     // Get a date time formatter for display in Lithuania 
     DateFormat fullDateFormatLT = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, localeLT); 

     System.out.println("Locale: "+localeFR.getDisplayName()); 
     System.out.println(fullDateFormatFR.format(date)); 
     System.out.println("Locale: "+localeLT.getDisplayName()); 
     System.out.println(fullDateFormatLT.format(date)); 
    } 

編輯: 工作的樣品從完成轉換日期美國格式:

@Test 
    public void testConvertDateFromFinToEn() throws Exception { 
     String dateInFin = "helmikuu 13, 2013"; 
     Locale localeFi = new Locale("fi", "FI"); 

     DateFormat dateFormatFi = new SimpleDateFormat("MMMM dd, yyyy", localeFi); 
     // Get Date object 
     Date date = dateFormatFi.parse(dateInFin); 

     // Convert to US date format 
     Locale localeUS = new Locale("en", "US"); 
     DateFormat dateFormatUS = new SimpleDateFormat("dd MMMM, yyyy", localeUS); 

     // Print in US format 
     System.out.println(dateFormatUS.format(date)); 
    } 
+0

我試圖使用相同的但沒有工作。 – Alex

+0

你會得到什麼錯誤?你能提供你的代碼嗎? –

+0

更新了主要問題請檢查。 – Alex

0

你可以使用SimpleDateFormat與本地

SimpleDateFormat 

public SimpleDateFormat(String pattern, 
         Locale locale) 

    Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the given locale. Note: This constructor may not support all locales. For full coverage, use the factory methods in the DateFormat class. 

    Parameters: 
     pattern - the pattern describing the date and time format 
     locale - the locale whose date format symbols should be used 
0

工作! !

   Locale localeFi = new Locale("fi", "FI"); 
       SimpleDateFormat dateFormatFin = new SimpleDateFormat("MMMMM dd, yyyy", localeFi); 
       System.out.println(dateFormatFin.parse(dateInFin));