String formatPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern( FormatStyle.FULL, null, IsoChronology.INSTANCE, loc); formatPattern = formatPattern.replaceFirst("^.*?([MLdEec].*[MLdEec]).*$", "$1"); DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(formatPattern, loc); System.out.println(LocalDate.now(ZoneId.of("Pacific/Johnston")).format(dateFormatter));输出
loc等于
Locale.US:
5月10日,星期四
与
Locale.UK(英国)一起:
5月10日,星期四
工作原理:我从一个本地化的格式模式字符串开始。在我的正则表达式中,我识别出与月份(
ML),月份中的月份(
d)和星期几(
Eec)有关的格式模式字母。我将此类字符串的开头到结尾保留子字符串。领先的勉强量词
.*?可确保我得到第一个匹配的字母。如果某些地区将年份设置在这些想要的元素之间,那么它将最终被包括在内。
我觉得自己太有创意了。在决定您想要这样的东西之前,请先测试您能想到的所有测试示例。



