Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何以区域设置敏感的方式显示工作日、月份、日期和年份_Java_Datetime_Localization - Fatal编程技术网

Java:如何以区域设置敏感的方式显示工作日、月份、日期和年份

Java:如何以区域设置敏感的方式显示工作日、月份、日期和年份,java,datetime,localization,Java,Datetime,Localization,在我的应用程序中,我需要以地区敏感的方式显示日期。因此,“2018年5月10日星期四”应按en_US的原样显示,但应按en_GB(英国)的原样显示为“2018年5月10日星期四” 在大多数情况下,我可以对java.time API类使用以下代码样式: public String toString(ZonedDateTime input) { DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(FormatStyle.MED

在我的应用程序中,我需要以地区敏感的方式显示日期。因此,“2018年5月10日星期四”应按en_US的原样显示,但应按en_GB(英国)的原样显示为“2018年5月10日星期四”

在大多数情况下,我可以对java.time API类使用以下代码样式:

public String toString(ZonedDateTime input) {
    DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(FormatStyle.MEDIUM, FormatStyle.SHORT);
    return input.format(dateTimeFormatter);
}

private DateTimeFormatter getDateTimeFormatter(FormatStyle dateStyle, FormatStyle timeStyle) {
    String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
            dateStyle, timeStyle, IsoChronology.INSTANCE, Locale.getDefault());

    return DateTimeFormatter.ofPattern(pattern);
}
在这种情况下,我不指定显式的日期模式,而是指定符号格式样式

我不确定处理没有标准格式样式满足我需求的情况的最佳方法

一个具体的例子是,我需要显示星期几、月份和日期,但不显示年份

因此,“2018年5月10日星期四”在en_US中应显示为“5月10日星期四”,但在en_GB(英国)中应显示为“5月10日星期四”


关于如何处理此要求有何建议?

尝试本地化日期格式化程序:

DateTimeFormatter pattern = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.US);
你可以用

DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
DayOfWeek.from(myDate).getDisplayName(TextStyle.FULL, Locale.getDefault())
将使用默认的系统区域设置。如果要选择显式区域设置(用于测试),则可以使用
with locale

DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
    .withLocale(Locale.US);
下面是一个例子:

DateTimeFormatter pattern = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
    .withLocale(Locale.US);

System.out.println(
    LocalDate.of(1999, 1, 1).format(pattern)
);
输出:
1999年1月1日

如果我将区域设置更改为
locale.UK
,则输出将变为
1999年1月1日


要获取星期几,可以使用

DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
DayOfWeek.from(myDate).getDisplayName(TextStyle.FULL, Locale.getDefault())
然后将字符串连接起来。(再次使用
Locale
查看不同的结果。
Locale.derman
提供
Freitag

输出时
loc
等于
Locale.US

5月10日,星期四

使用
Locale.UK
(英国):

5月10日,星期四

工作原理:我从本地化格式模式字符串开始。在正则表达式中,我识别与月份(
ML
)、月日(
d
)和星期日(
Eec
)有关的格式模式字母。我保留了从第一个字母到最后一个字母的子字符串。前导的不情愿量词
*?
确保我得到第一个匹配的字母。如果某个地区将年份放在这些想要的元素之间,它最终将被包括在内


我觉得自己太有创意了。在决定是否想要这样的东西之前,请使用您能想到的所有测试示例进行测试。

那么,在您的示例中,您不想要今年吗?我不认为有一个简单的方法,如果它适用于所有可思考的地方。是的,我不想要这一年。必须在所有地区工作。仅供参考,另一种只在一周中的某一天进行本地化的方法是。例如:
LocalDate.now().getDayOfWeek().getDisplayName(TextStyle.FULL,Locale.CANADA\u FRENCH)
@BasilBourque我更喜欢这样-更多的控制。谢谢