英文:
Why Date class does not support Internationalization?
问题
以下是翻译好的部分:
我正在研究在Java中获取日期和时间的不同方法,阅读文档时无法理解下面的引用是什么意思。
> 不幸的是,这些功能的API不适合国际化。
上述引用来自Java的Date类文档。这是什么意思?这是否是Date类的方法被弃用的原因?
英文:
I was going through the different ways that we can get Date and Time in java, while reading the documentation could not understand what the below quote mean.
>Unfortunately, the API for these functions was not amenable to internationalization.
The above quote is from documentation of Date class of java.
What does that mean ??
Is this the reason why the methods of the Date class were deprecated?
答案1
得分: 4
Sure, here is the translated content:
tl;dr
只使用 java.time 类。永远不要使用 Date
/Calendar
等。
ZonedDateTime.now(
ZoneId.of( "Asia/Tokyo" )
)
.format(
DateTimeFormatter
.ofLocalizedDateTime( FormatStyle.FULL )
.withLocale( Locale.GERMANY )
)
>2023年5月7日星期日,04:53:02 日本标准时间
避免使用遗留的日期时间类
你问道:
>这是为什么 Date 类的方法被弃用的原因吗?
遗留的日期时间类充满了糟糕的设计决策,是由不了解日期时间处理的人编写的。国际化不足仅仅是众多问题中的一个。
试图理解这些遗留类是浪费时间,除非你想学习如何不进行正确的面向对象编程。这些类早在几年前被现代的java.time 类所取代,这些类在JSR 310中定义。
既不要使用两个 Date
类,也不要使用 Calendar
、SimpleDateFormat
、GregorianCalendar
等。👉只使用java.time 包中的日期时间类。将你宝贵的学习时间集中在java.time 上。
java.time
java.time 类通过 👉 DateTimeFormatter
类提供国际化 支持。
在现代的Java中,本地化信息来自由 Unicode Consortium 发布的Common Locale Data Repository Project (CLDR) 提供的。
以下是一些示例代码。
捕获当前时刻,以 日本人民使用的挂钟时间/日历 为准。为此,使用 ZonedDateTime
类。使用 ZoneId
类指定感兴趣的时区。
ZoneId zoneIdTokyo = ZoneId.of( "Asia/Tokyo" );
ZonedDateTime zdtTokyo = ZonedDateTime.now( zoneIdTokyo );
使用法加蓝语本地化(人类语言和文化规范)生成表示该时刻的文本。
Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter formatter =
DateTimeFormatter
.ofLocalizedDateTime( FormatStyle.FULL )
.withLocale( locale );
String output = zdtTokyo.format( formatter );
输出到控制台。
System.out.println( "zdtTokyo.toString() = " + zdtTokyo );
System.out.println( "output = " + output );
运行时:
zdtTokyo.toString() = 2023-05-07T04:47:54.551174+09:00[Asia/Tokyo]
output = dimanche 7 mai 2023, 04 h 47 min 54 s heure normale du Japon
英文:
tl;dr
Use only java.time classes. Never use Date
/Calendar
etc.
ZonedDateTime.now(
ZoneId.of( "Asia/Tokyo" )
)
.format(
DateTimeFormatter
.ofLocalizedDateTime( FormatStyle.FULL )
.withLocale( Locale.GERMANY )
)
>Sonntag, 7. Mai 2023, 04:53:02 Japanische Normalzeit
Avoid legacy date-time classes
You asked:
> Is this the reason why the methods of the Date class were deprecated?
The legacy date-time classes are rife with poor design decisions, written by people who did not understand date-time handling. The lack of internationalization is but one of many problems.
Trying to understand these legacy classes is a waste of time, unless you want lessons on how to not do proper object-oriented programming. These classes were years ago supplanted by the modern java.time classes defined in JSR 310.
Use neither of the two Date
classes, nor Calendar
, SimpleDateFormat
, GregorianCalendar
, etc. 👉 Use only the date-time classes from the java.time package. Focus your precious learning time on java.time.
java.time
The java.time classes provide internationalization through the 👉 DateTimeFormatter
class.
In modern Java, the localization information comes from the Common Locale Data Repository Project (CLDR) published by the Unicode Consortium.
Here is some example code.
Capture the current moment as seen in the wall-clock time/calendar used by the people of Japan. For this purpose, use ZonedDateTime
class. Specify the time zone of interest by using the ZoneId
class.
ZoneId zoneIdTokyo = ZoneId.of( "Asia/Tokyo" );
ZonedDateTime zdtTokyo = ZonedDateTime.now( zoneIdTokyo );
Generate text representing that moment using French Canadian localization (human language & cultural norms).
Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter formatter =
DateTimeFormatter
.ofLocalizedDateTime( FormatStyle.FULL )
.withLocale( locale );
String output = zdtTokyo.format( formatter );
Dump to console.
System.out.println( "zdtTokyo.toString() = " + zdtTokyo );
System.out.println( "output = " + output );
When run:
zdtTokyo.toString() = 2023-05-07T04:47:54.551174+09:00[Asia/Tokyo]
output = dimanche 7 mai 2023, 04 h 47 min 54 s heure normale du Japon
答案2
得分: 0
如果您查看建议的替代项,您会看到有两个 - Calendar 和 GregorianCalendar。
程序员需要根据代码的意图做出选择。旧的 API 将会是其中之一。也就是说,它 "不支持国际化"。
英文:
If you look at the suggested replacements, you'll see there are two - Calendar and GregorianCalendar.
The programmer needs to make a choice, depending on the intention of the code. The old API would have been one or the other. That is, it "did not support internationalization".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论