Java 用西班牙语分析日期错误
您好,请帮助我,我已经通过了许多问题,但没有得到一个解决方案。 代码 错误Java 用西班牙语分析日期错误,java,android,date,date-parsing,Java,Android,Date,Date Parsing,您好,请帮助我,我已经通过了许多问题,但没有得到一个解决方案。 代码 错误 请检查这行的拼写 String localDate1="Miércoles, 04 Octubre 2017 12:00 PM"; 将十月改为八方,并选中此米克尔您可以使用此代码作为参考: 此代码转换为:—— 米尔科莱斯,2017年10月4日上午12:00 到 2017年10月4日星期三00:00:00 IST import java.io.IOException; import java.text.ParseExce
请检查这行的拼写
String localDate1="Miércoles, 04 Octubre 2017 12:00 PM";
将十月改为八方,并选中此米克尔您可以使用此代码作为参考: 此代码转换为:—— 米尔科莱斯,2017年10月4日上午12:00 到 2017年10月4日星期三00:00:00 IST
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class test {
public static void main(String[] args) throws IOException, ParseException {
//Wednesday, October 4, 2017
String dateInString = "4-Oct-2017";
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date = formatter.parse(dateInString);
SimpleDateFormat formato = new SimpleDateFormat("EEEE, dd MMMM yyyy hh:mm aaaa", new Locale("es", "ES"));
String fecha = formato.format(date);
System.out.println(fecha);
String localDate1 = fecha;
Locale spanishLocale = new Locale("es", "ES");
String pattern = "E, dd MMMM yyyy hh:mm aaaa";
SimpleDateFormat spanishLocale1 = new SimpleDateFormat(pattern, spanishLocale);
String dateInSpanish = spanishLocale1.parse(localDate1).toString();
System.out.println(dateInSpanish);
}
}
请记录在案:
幸运的是,您已经发布了指向偏移量33(即输入中“PM”的位置)的错误消息。因此,我们可以说:
您的问题与依赖于设备的本地化数据有关(或依赖于操作系统),这里是AM/PM西班牙语表示形式的具体数据。在旧版本的CLDR unicode repository(作为许多Java、C#或Android发行版的通用源的行业标准)中,使用了数据“AM”和“PM”,但在新版本中,西班牙语使用“a.m.”或“p.m.”
因此,如果要分析的输入(包含“PM”)与您拥有的真实i18n数据不匹配,我建议将其作为实用的解决方案字符串预处理:
String input = "Miércoles, 04 Octubre 2017 12:00 PM";
input = input.replace("PM", "p. m.");
// now parse your input with Spanish locale and the appropriate pattern
getString(R.string.jom\u events\u date\u input\u format\u 12\u hrs)的值是多少?
EEEE,dd-MMMM-yyyyy hh:mm a,当我解析英语日期时,它工作得很好,虽然我的默认区域设置也是“es”,但解析应该使用输入中的“p.m.”而不是“PM”,前提是您的模式确实是“EEEE,dd-MMMM-yyyyy hh:mm a”您还可以使用宽松解析(不区分大小写)。请记住,您的错误消息指向偏移量33,即“PM”-部分的位置。@MenoHochschild更改为“p.m.”实际上有效。它没有抛出错误,谢谢。它实际上是服务器生成的日期,google将其翻译为2017年10月4日星期三12:00 PM月份名称和工作日名称可以用西班牙语表示(撇开它不希望大写,但解析器不区分大小写的事实不谈——默认情况下是宽松的)。
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class test {
public static void main(String[] args) throws IOException, ParseException {
//Wednesday, October 4, 2017
String dateInString = "4-Oct-2017";
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date = formatter.parse(dateInString);
SimpleDateFormat formato = new SimpleDateFormat("EEEE, dd MMMM yyyy hh:mm aaaa", new Locale("es", "ES"));
String fecha = formato.format(date);
System.out.println(fecha);
String localDate1 = fecha;
Locale spanishLocale = new Locale("es", "ES");
String pattern = "E, dd MMMM yyyy hh:mm aaaa";
SimpleDateFormat spanishLocale1 = new SimpleDateFormat(pattern, spanishLocale);
String dateInSpanish = spanishLocale1.parse(localDate1).toString();
System.out.println(dateInSpanish);
}
}
String input = "Miércoles, 04 Octubre 2017 12:00 PM";
input = input.replace("PM", "p. m.");
// now parse your input with Spanish locale and the appropriate pattern