Search code examples
javadatepickerdate-formatarabicswingx

JXDatePicker swingx language


Modified question How to make JXDatePicker use a specified language ?


Solution

  • Unfortunately JXDatePicker is dependent on the old Java date and time classes including Date and DateFormat. It’s unfortunate because they are poorly designed and have later (5 years ago) been replaced with java.time, the modern Java date and time API. You may want to research whether you can find a more modern date picker component to replace it.

    Failing that, JXDatePicker.setFormats does require either DateFormat objects or String objects. The reasonable solution is to pass a Locale to your SimpleDateFormat before passing it to the date picker:

        DateFormat dateFormat = new SimpleDateFormat("E, yyyy-MM-dd", Locale.ENGLISH);        
    

    For formatting the Date that you get from the date picker you have the option of converting it to a modern type before formatting it:

        DateTimeFormatter dateFormatter = DateTimeFormatter
                .ofPattern("E, yyyy-MM-dd", Locale.ENGLISH);
        Date oldfashionedDate = DateDP.getDate();
        ZonedDateTime dateTime = oldfashionedDate.toInstant().atZone(ZoneId.systemDefault());
        String dateString = dateTime.format(dateFormatter);
        System.out.println(dateString);
    

    Specifying English locale for the DateTimeFormatter makes sure that the formatted date will have the abbreviation for day of week in English, for example:

    Mon, 2019-08-05

    Links