问题描述
我通过使用SimpleDateFormat
格式化日期来保存日期.
I have saved dates by formatting them using SimpleDateFormat
.
DateFormat dateForm = new SimpleDateFormat("HH mm ss dd MMM ''yy");
String dateOutput = dateForm.format(new Date());
这使用设备的默认语言环境,例如法语或西班牙语.
This uses the default Locale of the device, for example French or Spanish.
如何将这个字符串(针对不同的语言环境设置格式)转换回以Locale.ENGLISH
格式设置的Date
对象?
How do I convert this string (formatted for a different Locale) back to a Date
object formatted with Locale.ENGLISH
?
当前,当我尝试将字符串转换回日期时,出现不可解析的日期异常.这是由于使用不同的语言环境保存了日期
Currently when I try to convert the string back into a date, I get an unparseable date exception. This is caused by the fact the the date was saved using a different Locale
推荐答案
这可以为您服务
SimpleDateFormat sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.ENGLISH);
String dateOutput = sdf.format(new Date());
编辑
我想的方法是用Calendar
对象将其分解并用另一种语言格式化.试试这个
The way I am thinking is to break it down with Calendar
object and formatting it in another language. Try this
SimpleDateFormat sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.getDefault());
Calendar cal = Calendar.getInstance();
int day = 0, month = 0, year = 0, hour = 0, minute = 0, sec = 0;
String loc = Locale.getDefault().getDisplayLanguage();
try {
Date testDate = sdf.parse(date);
cal.setTime(testDate);
sec = cal.get(Calendar.SECOND);
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
} catch (ParseException e) {
if(loc.equals(Locale.ENGLISH.getDisplayLanguage())){
sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.FRENCH);
try {
Date testDate = sdf.parse(date);
cal.setTime(testDate);
sec = cal.get(Calendar.SECOND);
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
} catch (ParseException ee) {
sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.FRENCH);
try {
Date testDate = sdf.parse(date);
cal.setTime(testDate);
sec = cal.get(Calendar.SECOND);
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
} catch (ParseException eex) {
}
}
}
else if (loc.equals(Locale.FRENCH.getDisplayLanguage())){
sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.ENGLISH);
try {
Date testDate = sdf.parse(date);
cal.setTime(testDate);
sec = cal.get(Calendar.SECOND);
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
} catch (ParseException fe) {
}
}
}
cal.set(Calendar.SECOND, sec);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.HOUR, hour);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
if(loc.equals(Locale.ENGLISH.getDisplayLanguage()))
sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.getDefault());
else if(loc.equals(Locale.FRENCH.getDisplayLanguage()))
sdf = new SimpleDateFormat("HH mm ss dd MMM ''yy", Locale.getDefault());
Date convertedLangDate = cal.getTime();
String newDate = sdf.format(convertedLangDate);
这确实对我有用,但这并不是最优雅的解决方案. Tweek为您的代码.
This does work for me however it is not the most elegant of solutions. Tweek it for your code.
这篇关于将日期从默认语言环境转换为英语语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!