读取不同语言环境的时间戳
read timestamp which is in a different locale
"timestamp_utc": "۲۰۱۵-۱۱-۰۲T۱۸:۴۴:۳۴+۰۰:۰۰"
是 JSON 中的一个属性。我如何解析这个日期?
我尝试了以下代码。
try
{
return new DateTime(dateStr, DateTimeZone.UTC);
}
catch (IllegalArgumentException e)
{
java.util.Locale locale = new java.util.Locale( "ar", "SA" );
DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withLocale( locale );
return formatter.parseDateTime( dateStr );
}
2015-05-11T11:31:47
工作正常。然而,
۲۰۱۵-۱۱-۰۲T۱۸:۴۴:۳۴+۰۰:۰۰
抛出 IllegalArgumentException
。也尝试用其他 locales/formats 解析日期。没有运气。
Locale list[] = DateFormat.getAvailableLocales();
for (Locale aLocale : list) {
try{
DateTimeFormatter formatter = DateTimeFormat.forPattern( "yyyy-MM-dd'T'HH:mm:ssZ" ).withLocale(aLocale);
System.out.println(formatter.parseDateTime( dateStr ));
}
catch(Exception e){
System.out.println("locale " + aLocale.toString() + " error");
}
}
请帮帮我。
添加非阿拉伯字符 (T) 使其成为非阿拉伯日期(我通过尝试在 google translate 中翻译它得到了这个想法)。尝试以下操作(在输入日期和模式中将 T 更改为 ):
public static void main (String[] args) throws java.lang.Exception
{
String ara = "۲۰۱۵-۱۱-۰۲ ۱۸:۴۴:۳۴+۰۰:۰۰";
for (Locale aLocale : DateFormat.getAvailableLocales()) {
//Just to save time, not needed.
if(aLocale.getLanguage() != "ar") continue;
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+SS:SS", aLocale);
System.out.println(sdf.parse( ara ));
}
catch(Exception e){
System.out.println("locale " + aLocale.toString() + " error");
}
}
}
"timestamp_utc": "۲۰۱۵-۱۱-۰۲T۱۸:۴۴:۳۴+۰۰:۰۰"
是 JSON 中的一个属性。我如何解析这个日期? 我尝试了以下代码。
try
{
return new DateTime(dateStr, DateTimeZone.UTC);
}
catch (IllegalArgumentException e)
{
java.util.Locale locale = new java.util.Locale( "ar", "SA" );
DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withLocale( locale );
return formatter.parseDateTime( dateStr );
}
2015-05-11T11:31:47
工作正常。然而,
۲۰۱۵-۱۱-۰۲T۱۸:۴۴:۳۴+۰۰:۰۰
抛出 IllegalArgumentException
。也尝试用其他 locales/formats 解析日期。没有运气。
Locale list[] = DateFormat.getAvailableLocales();
for (Locale aLocale : list) {
try{
DateTimeFormatter formatter = DateTimeFormat.forPattern( "yyyy-MM-dd'T'HH:mm:ssZ" ).withLocale(aLocale);
System.out.println(formatter.parseDateTime( dateStr ));
}
catch(Exception e){
System.out.println("locale " + aLocale.toString() + " error");
}
}
请帮帮我。
添加非阿拉伯字符 (T) 使其成为非阿拉伯日期(我通过尝试在 google translate 中翻译它得到了这个想法)。尝试以下操作(在输入日期和模式中将 T 更改为
public static void main (String[] args) throws java.lang.Exception
{
String ara = "۲۰۱۵-۱۱-۰۲ ۱۸:۴۴:۳۴+۰۰:۰۰";
for (Locale aLocale : DateFormat.getAvailableLocales()) {
//Just to save time, not needed.
if(aLocale.getLanguage() != "ar") continue;
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+SS:SS", aLocale);
System.out.println(sdf.parse( ara ));
}
catch(Exception e){
System.out.println("locale " + aLocale.toString() + " error");
}
}
}