Java 中的本地化相对日期格式

发布于 2024-12-29 13:36:29 字数 494 浏览 1 评论 0原文

我想显示这样的日期:

[相对],短月日年小时:分钟 [AM|PM] [时区]

对于 Locale.ENGLISH 例如:

Today, Jan 26, 2012 07:41 PM CET

对于 Locale GERMAN 例如:

Heute, 26. Jan 2012 19:41 MEZ

对于几天前的日期:

Jan 22, 2012 07:41 PM CET

因此,[RELATIVE]-“前缀”应该仅在“可用”时出现(使用 ICU4J),否则我们想查看确切的日期;我们希望根据区域设置自动从 12/24 小时表示法切换 模式

不起作用,因为没有该相关事物的标识符,也没有基于区域设置自动 12/24 小时的 hh:mm 标识符。

有什么想法吗?

I would like to display dates like this:

[RELATIVE], SHORT-MONTH DAY YEAR HOURS:MINS [AM|PM] [TIMEZONE]

For Locale.ENGLISH e.g.:

Today, Jan 26, 2012 07:41 PM CET

For Locale GERMAN e.g.:

Heute, 26. Jan 2012 19:41 MEZ

For a date several days ago just:

Jan 22, 2012 07:41 PM CET

So, the [RELATIVE]-"prefix" should only appear if "available" (using ICU4J), otherwise we would like to see the exact date; we want do switch from a 12/24 hours representation automatically based on the locale

Patterns do not work since there is no identifier for that relative thing and also none for hh:mm with automatic 12/24 hours based on the locale.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

貪欢 2025-01-05 13:36:29

看看

DateFormat.getDateInstance(int style, Locale locale)

Javadoc

与此您可以获得与区域设置相关的格式字符串。我不知道有任何合适的 API 可以在您的约会对象面前提供今天/明天 - Heute/Morgen。但您可以轻松地在前面提供星期几,例如 Wed 01/01/2010 10:00PM。

DateFormat 支持 12 小时(含 AM/PM)或一天 24 小时。


更新您的评论:再次检查 javadoc,DateFormat 支持 AM/PM 或全天时间,具体取决于区域设置。

    System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL,
            DateFormat.FULL, Locale.ENGLISH).format(
            Calendar.getInstance().getTime()));
    System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL,
            DateFormat.FULL, Locale.GERMAN).format(
            Calendar.getInstance().getTime()));

生成

Thursday, January 26, 2012 9:49:41 PM CET
Donnerstag, 26. Januar 2012 21:49 Uhr MEZ

留下今天/明天的问题。 SimpleDateFormat 不提供此功能。如果您确实需要它,我会编写一个助手,它接受输入参数,包装 getDateTimeInstance().format() 调用,并在其前面添加今天/明天(如果日期匹配)。

Have a look at

DateFormat.getDateInstance(int style, Locale locale)

Javadoc

With this you can get locale dependent format strings. I'm not aware of any proper API to provide Today/Tomorrow - Heute/Morgen in front of your date. But you could easily provide the day of week in front, like Wed 01/01/2010 10:00PM.

DateFormat supports both, 12 with AM/PM or 24 hours a day.


Update to your comment: Check the javadoc again, DateFormat supports AM/PM or full hours of day depending on locale.

    System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL,
            DateFormat.FULL, Locale.ENGLISH).format(
            Calendar.getInstance().getTime()));
    System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL,
            DateFormat.FULL, Locale.GERMAN).format(
            Calendar.getInstance().getTime()));

generates

Thursday, January 26, 2012 9:49:41 PM CET
Donnerstag, 26. Januar 2012 21:49 Uhr MEZ

Leaves the today/tomorrow issue. SimpleDateFormat does not provide this feature. If you really need it, I'd write a helper that takes the input parameters, wraps the getDateTimeInstance().format() call and adds Today/Tomorrow in front of it, if the date matches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文