7
"timestamp_utc": "۲۰۱۵-۱۱-۰۲T۱۸:۴۴:۳۴+۰۰:۰۰"

is an attribute in a JSON. How do I parse this date? I tried the following piece of code.

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 Works just fine. However, ۲۰۱۵-۱۱-۰۲T۱۸:۴۴:۳۴+۰۰:۰۰ throws an IllegalArgumentException. Tried parsing the date with other locales/formats as well. No luck.

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");
            }
        }

Please help me out.

Karthick R
  • 599
  • 9
  • 25
  • That data format may simply not be supported by java data libraries, have you looked into the source code whether arabic parsing is supported? – randers Dec 09 '15 at 15:39

1 Answers1

2

Adding a non-Arabic character (T) made it a non-Arabic date (I got the idea by trying to translate it in google translate). Try the below (Changed T to <space> in both input date and the pattern):

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");
        }
    }
}
Vasan
  • 4,810
  • 4
  • 20
  • 39