Search code examples
javaarabicjava-timelocaldatedate-parsing

Converting a string with Arabic characters to a date


I'm trying to verify that a list of items are sorted according their date desc through Java and appium. I managed to extract the dates from the screen as a String, but I'm facing a difficulty in converting these strings into dates because the string is basically containing an Arabic date, like the following: يناير ٧ ٢٠٢٠

I've tried to use the code below,

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu d MMMM ", new Locale("ar"));
        LocalDate orderDate = LocalDate.parse(date, formatter);

However, Im getting the following error:

java.time.format.DateTimeParseException: Text 'يناير ٧ ٢٠٢٠' could not be parsed at index 0

    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    at java.base/java.time.LocalDate.parse(LocalDate.java:428)
    at com.hs.mobile.steps.MyOrdersSteps.getDate(MyOrdersSteps.java:142)
    at com.hs.mobile.steps.MyOrdersSteps.getOrdersDates(MyOrdersSteps.java:133)
    at com.hs.mobile.steps.MyOrdersSteps.verifyOrdersSortedByDateDesc(MyOrdersSteps.java:119)
    at com.hs.mobile.tests.MyOrdersTests.navigateToOrders_OrdersShouldBeSortedByDate(MyOrdersTests.java:30)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
    at org.testng.TestRunner.privateRun(TestRunner.java:770)
    at org.testng.TestRunner.run(TestRunner.java:591)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:41)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:443)
    at org.testng.internal.thread.ThreadUtil.lambda$execute$0(ThreadUtil.java:67)
    at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

Can you please advise in a proper solution for this issue?

Thanks


Solution

  • pls check out my solution, it is really very tricky to convert from ar to another locale because when you read from arabic you have to read text from the right to left like this:

    the -> "يناير ٧ ٢٠٢٠" will be: january 7 2020  
    

    so DateFormatter will look like this:

    SimpleDateFormat sdf =
                new SimpleDateFormat("MMMM d yyyy", Locale.forLanguageTag("ar-SA-nu-arab"));
    

    and then parse it to the date:

    Date d = sdf.parse(date);
    

    and print it:

            System.out.println(d);
    

    it will print this:

    Tue Jan 07 00:00:00 MSK 2020