将非英语日期转换为标准Java日期
我正在进行一个项目,我需要将非英语日期转换为标准JAVA日历/日期格式。例如,日期如2013年2月13日应转换为2013年2月13日 我认为使用Java简单的日期格式定位函数是可行的。我试图使用此代码,但它抛出了不可解析的日期:“helmikuu 13,2013”错误将非英语日期转换为标准Java日期,java,date-format,Java,Date Format,我正在进行一个项目,我需要将非英语日期转换为标准JAVA日历/日期格式。例如,日期如2013年2月13日应转换为2013年2月13日 我认为使用Java简单的日期格式定位函数是可行的。我试图使用此代码,但它抛出了不可解析的日期:“helmikuu 13,2013”错误 public static void main(String args[]) throws Exception { String dateInFin = "helmikuu 13, 2013"; L
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));
}
使用
java.text.DateFormat
:
DateFormat format = new SimpleDateFormat("MMMM d, yyyy");
使用
parse(String)
和format()
方法解析和格式化日期。使用java.text.DateFormat
:
DateFormat format = new SimpleDateFormat("MMMM d, yyyy");
使用
parse(String)
和format()
方法来解析和格式化日期。例如,您可以这样做(示例是如何从法语区域设置转换为Lihuanian):
编辑:
将完工日期转换为美国格式的工作样本:
@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));
}
例如,您可以这样做(示例是如何从法语区域设置转换为Lihuanian): 编辑: 将完工日期转换为美国格式的工作样本:
@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));
}
您可以与本地用户一起使用
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
您可以与本地用户一起使用
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
成功了
Locale localeFi = new Locale("fi", "FI");
SimpleDateFormat dateFormatFin = new SimpleDateFormat("MMMMM dd, yyyy", localeFi);
System.out.println(dateFormatFin.parse(dateInFin));
成功了
Locale localeFi = new Locale("fi", "FI");
SimpleDateFormat dateFormatFin = new SimpleDateFormat("MMMMM dd, yyyy", localeFi);
System.out.println(dateFormatFin.parse(dateInFin));
但是我的日期字符串不是英文的,我怎样才能使用相同的函数。但是我的日期字符串不是英文的,我怎样才能使用相同的函数。