时区日期时间本地化

huangapple go评论72阅读模式
英文:

ZonedDateTime localization

问题

根据稍后获取的语言环境,我需要输出 ZonedDateTime。有没有办法可以将 ZonedDateTime 的值转换为所需的格式?

ZonedDateTime creationDate = ZonedDateTime.now();
// 根据现有的语言环境转换 creationDate
英文:

I need to output ZonedDateTime according to the locale I get later. Is there any way I can convert the ZonedDateTime value to the required format?

ZonedDateTime creationDate=ZonedDateTime.now();
//convert creationDate depending on the existing locale

答案1

得分: 1

ZonedDateTime没有一个固定的格式。ZonedDateTime的概念是无格式的。

格式化器是它们自己的对象(DateTimeFormatter的实例)。它们是可配置的(例如,您可以更改它们的区域设置,它们执行的所有区域设置特定渲染,例如长格式月份名称,将会更改),并且您可以要求它们格式化所提供的ZonedDateTime。

因此,您可以在任何时候创建您的ZonedDateTime对象,将它存储在任何地方。如果在3天后,您需要根据刚刚获取的某个区域设置进行格式化,那么可以这样做:

ZonedDateTime zdt = ZonedDateTime.now();
// 经过了几天

DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).localizedBy(Locale.FRENCH);

System.out.println(dtf.format(zdt));
英文:

ZonedDateTime doesn't have a format. The concept of a ZonedDateTime is format-less.

Formatters are their own objects (instances of DateTimeFormatter). They are configurable (for example, you can change their locale, and all the locale-specific rendering they do, such as long-form month names, will then change), and you can ask them to format a provided zoneddatetime.

Thus, make your zoneddatetime object whenever you want, store it whereever you want. If, 3 days later, you need to format it according to some locale you just now got, great. Do that then:

ZonedDateTime zdt = ZonedDateTime.now();
// days pass

DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).localizedBy(Locale.FRENCH);

System.out.println(dtf.format(zdt));

huangapple
  • 本文由 发表于 2020年9月10日 20:38:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63829858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定