0

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

我认为使用 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));

    }
4

4 回答 4

4

例如,您可以这样做(例如如何从法语语言环境转换为立欢语):

@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));
    }
于 2013-10-18T07:03:19.717 回答
0

使用java.text.DateFormat

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

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

于 2013-10-18T07:01:36.803 回答
0

工作!!

                Locale localeFi = new Locale("fi", "FI");
                SimpleDateFormat dateFormatFin = new SimpleDateFormat("MMMMM dd, yyyy", localeFi);
                System.out.println(dateFormatFin.parse(dateInFin));
于 2013-10-18T07:52:32.923 回答
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 
于 2013-10-18T07:08:20.370 回答