How to Parse date in Simple date format in arabic locale?
Asked Answered
P

2

8

i have date coming from server & is in the format = "2013-01-20T16:48:43" my application support Both Arabic & English Locale. But when i change the locale to Arabic the date is not parsing its giving me parse exception. till now what i have written is

private static Date parseJsonDate(final String string) throws Exception
    {
    final String change_Locale = Locale.getDefault().getISO3Language();
            if (change_Locale.equalsIgnoreCase("ara"))
            {

                System.out.println(":: Date :::" + string);
                final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", new Locale("ar"));

                System.out.println("new date " + format.parse(string));
                return format.parse(string);
Peep answered 21/1, 2013 at 13:5 Comment(2)
I cannot reproduce. I get Sun Jan 20 16:48:43 CET 2013.Poole
For new readers to the question I strongly recommend that you do not use SimpleDateFormat. That class was notoriously troublesome and is long outdated. Use LocalDateTime from java.time, the modern Java date and time API. It can parse your string without the need for specifying any formatter.Poole
T
13

Do not parse your date into the Arabic it will give you error alwayz besides try as below by setting the Locale ENGLISH only.

final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
Tripp answered 21/1, 2013 at 13:11 Comment(2)
@ Grishu Locale.Arabic is not supported in javaPeep
, Locale.ENGLISH this on app language change not workingNilson
G
4

Your error seems to be due to some bug in the older version of Java.

Note that in March 2014, the modern Date-Time API supplanted the legacy date-time API and since then it is strongly recommended to switch to java.time, the modern date-time API.

The java.time API has a specialised type, LocalDateTime to represent an object that has just date and time units, and no timezone information.

Demo:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) throws ParseException {
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", new Locale("ar"));
        LocalDateTime ldt = LocalDateTime.parse("2013-01-20T16:48:43", parser);
        System.out.println(ldt);

        // Alternatively, as suggested by Basil Bourque
        parser = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withLocale(new Locale("ar"));
        System.out.println(LocalDateTime.parse("2013-01-20T16:48:43", parser));

        // Your parser
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", new Locale("ar"));
        System.out.println(format.parse("2013-01-20T16:48:43"));
    }
}

Output:

2013-01-20T16:48:43
2013-01-20T16:48:43
Sun Jan 20 16:48:43 GMT 2013

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Godown answered 31/12, 2022 at 18:29 Comment(5)
Thanks, @BasilBourque. I have incorporated your suggestion in my answer.Godown
@BasilBourque We don’t even need any explicit formatter. LocalDateTime.parse("2013-01-20T16:48:43") does it.Poole
Thanks, @OleV.V. It was necessary for demonstrating the use of Locale.Godown
@ArvindKumarAvinash Yes, I did not know if calling withLocale on the constant would work or not.Luckin
Do we really need a Locale at all here? If we have a String object such as "2013-01-20T16:48:43" in hand, then we are simply parsing Unicode text. There is nothing localized about it, nothing to do with Arabic specifically.Luckin

© 2022 - 2024 — McMap. All rights reserved.