如何在Java 8中自动本地化日期格式 [英] How to localise date format automatically in Java 8

查看:67
本文介绍了如何在Java 8中自动本地化日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看日期和Java 8中的时间库,想知道是否有一种方法可以将日期格式化为本地格式,而不管程序在何处运行(例如,使用计算机设置为该区域的本地格式)

I've been looking at the Date and Time library in Java 8 and was wondering if there is a way to format the date to the local format regardless of where the program is being run (as in, using the local format of the region the computer is set to).

日期和时间库使用LocalDate类获取日期.默认情况下,使用LocalDate.now()使用yyyy-MM-dd格式.
可以使用DateTimeFormatter对其进行格式化,如下所示:

The Date and Time library uses the LocalDate class for getting a date. Using LocalDate.now() uses the yyyy-MM-dd format by default.
This can be formatted using a DateTimeFormatter like so:

DateTimeFormatter dtFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.now().format(dtFormatter);

不幸的是,这完全取决于我在ofPattern(pattern)方法中输入的模式.
还有一种ofPattern(pattern, locale)方法,该方法允许使用语言环境,但是使用下面的代码并不能格式化,因为它不会将日期转换为MM-dd-yyyy的美国格式:

Unfortunately, this relies entirely on the pattern I enter in the ofPattern(pattern) method.
There is also a ofPattern(pattern, locale) method which allows a locale to be used, however using the following code does not format as I would like as it does not convert the date to the US format of MM-dd-yyyy:

DateTimeFormatter dtFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.US);
LocalDate date = LocalDate.now().format(dtFormatter);

如果这行得通,我会尝试使用Locale.getDefault().不幸的是,它似乎并没有改变任何东西.

If this did work, I would try using Locale.getDefault(). Unfortunately it doesn't appear to change anything.

那么有什么方法可以使用Java 8日期和时间库将日期转换为程序当前正在运行的位置所使用的格式.例如,如果我在美国,我将获得MM-dd-yyyy格式;但是如果我的计算机的语言环境设置为英国,则日期将为dd-MM-yyyy.

So is there any way using the Java 8 Date and Time library to convert a date into the format used by the location that the program is currently running. For example, if I was in the US, I would get the MM-dd-yyyy format; but if my computer's locale was set to the UK, the date would be given as dd-MM-yyyy.

编辑:我忘了提,我已经尝试过为此问题提供的解决方案,但是此格式将日期设置为dd/MM/yy,但是我希望将其设置为dd-MM-yyyy.我可以使用replaceAll("/","-")将每个/更改为-,但这不会将年份从yy转换为yyyy格式.

I forgot to mention, I've tried the solution given to this question, however this formats the date as dd/MM/yy, however I would prefer it as dd-MM-yyyy. I can change each / into a - using replaceAll("/","-"), however this does not convert the year from yy to yyyy format.

推荐答案

您的搜索提出了正确的解决方案:

Your search brought up the correct solution:

    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
    System.out.println(LocalDate.now().format(dateFormatter));

我将JVM的语言环境设置为Locale.US,立即运行此代码并得到:

I set my JVM’s locale to Locale.US, ran this code just now and got:

2019年1月17日

Jan 17, 2019

您可以选择所需格式的长度或长度.如果我们使用FormatStyle.SHORT而不是FormatStyle.MEDIUM,则正确的是我们只能得到两位数的年份:

You can choose how long or how short of a format you want. If we use FormatStyle.SHORT instead of FormatStyle.MEDIUM, you are correct that we get only two digit year:

1/17/19

1/17/19

问题的关键在于它随地区而异:在其他地区中,您会得到不同的结果,并且可能有些年份会给您带来四位数的年份.因此,您可能认为这对美国来说是一个极端的案例.是否要在代码中设置例外并特别对待美国语言环境,将取决于这对您和您在美国的用户有多重要.

The point is exactly that it varies by locale: in other locales you will get different results, and probably some will give you four digit year. So you may consider this a corner case for US. Whether you want to make an exception in your code and treat US locale specially — will have to depend on how important this is to you and to your users in the US.

一方面,一些工作已放入本地化格式,所以对于大多数目的,我倾向于更信任它们,而不是相信自己对用户的偏爱.另一方面,有时可能需要考虑一些特殊因素,这些因素表明内置的本地化格式并不完全适合特定情况.如果是后者,那么跨语言环境通常就是这种情况.我的直觉表明您可能不会遇到这种情况,因此我的第一个建议是,看看您是否可以使用内置的SHORTMEDIUM格式.

On one hand some work has been put into the localized formats, so for most purposes I tend to trust them more than I trust my own idea of what I think my users prefer. On the other hand there may sometimes be special considerations that dictate that a built-in localized format isn’t exactly right for a particular situation. When the latter is the case, it’s usually the case across locales. My gut feeling says that you may not be in such a situation, so my first suggestion is you see if you can live with either the built-in SHORT or MEDIUM format.

只需使用FormatStyle.MEDIUM.我刚刚注意到您在新西兰.因此,我将JVM的语言环境设置为Locale.forLanguageTag("en-NZ").现在使用FormatStyle.SHORT,您将得到17/01/19的报告.但是有了FormatStyle.MEDIUM,我得到的是17/01/2019,也就是四位数的年份,总的来说,很接近您想要的内容.

Just use FormatStyle.MEDIUM. I just noticed you’re in New Zealand. So I set my JVM’s locale to Locale.forLanguageTag("en-NZ") instead. Now with FormatStyle.SHORT I get 17/01/19 as you reported. But with FormatStyle.MEDIUM I get 17/01/2019, that is, four digit year and all in all pretty close to what you said you wanted.

这篇关于如何在Java 8中自动本地化日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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