JXDatePicker swingx语言

问题描述 投票:0回答:1

修改的问题如何使JXDatePicker使用指定的语言?

java datepicker date-format arabic swingx
1个回答
1
投票

[不幸的是JXDatePicker取决于旧的Java日期和时间类,包括DateDateFormat。不幸的是,由于它们的设计不佳,后来(5年前)被现代Java日期和时间API java.time取代。您可能想研究是否可以找到更现代的日期选择器组件来替换它。

[失败,JXDatePicker.setFormats确实需要DateFormat对象或String对象。合理的解决方案是在将Locale传递给日期选择器之前将其传递给SimpleDateFormat

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

用于格式化从日期选择器获得的Date,可以选择在格式化之前将其转换为现代类型:

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

DateTimeFormatter指定英语语言环境可确保格式化日期的英文缩写为星期几,例如:

2019年5月1日星期一

链接

© www.soinside.com 2019 - 2024. All rights reserved.