获取Java中特定于语言环境的日期/时间格式 [英] Get locale specific date/time format in Java

查看:70
本文介绍了获取Java中特定于语言环境的日期/时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中有用例,我们想要获取特定于语言环境的日期.我正在使用DateFormat.getDateInstance

I have use case in java where we want get the locale specific date. I am using DateFormat.getDateInstance

final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM,
                Locale.forLanguageTag(locale)));

这将转换日期,但是ja-JP会将日期"2019年1月17日"转换为"2019/01/17",但我需要类似"2019年1月17日"的名称.对于所有其他语言环境,这可以正确翻译日期.

This translates the dates but ja-JP this translates the date "17 January 2019" to "2019/01/17" but I need something like "2019年1月17日". For all other locales this correctly translates the date.

请告知是否还有其他方法可以做到这一点.

Please let know if there is other method to get this.

推荐答案

这对我有用:

public static void main(String[] args) throws IOException {
    final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.JAPAN);
    Date today = new Date();
    System.out.printf("%s%n", dateFormat.format(today));
}

MEDIUM 完全按照您的说法

UPD:或使用更新的 ZonedDataTime 正如迈克尔·甘特曼(Michael Gantman)所建议的:

UPD: or using newer ZonedDataTime as Michael Gantman suggested:

public static void main(String[] args) throws IOException {
    ZonedDateTime zoned = ZonedDateTime.now();
    DateTimeFormatter pattern = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(Locale.JAPAN);
    System.out.println(zoned.format(pattern));
}

这篇关于获取Java中特定于语言环境的日期/时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆