将非英语日期转换为标准Java日期

时间:2013-10-18 06:57:18

标签: java date-format

我正在开展项目,我需要将非英语日期转换为标准JAVA日历/日期格式。例如,helmikuu 13, 2013之类的日期应转换为2013年2月13日。

我认为它可以使用Java Simple Date Format Locate Function。我尝试使用此代码,但会引发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));

    }

4 个答案:

答案 0 :(得分: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));
    }

答案 1 :(得分:0)

使用java.text.DateFormat

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

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

答案 2 :(得分: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 

答案 3 :(得分:0)

工作! !

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