问题
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).
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);
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);
If this did work, I would try using Locale.getDefault()
. Unfortunately it doesn't appear to change anything.
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.
EDIT: 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.
回答1:
Your search brought up the correct solution:
DateTimeFormatter dateFormatter
= DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
System.out.println(LocalDate.now().format(dateFormatter));
I set my JVM’s locale to Locale.US
, ran this code just now and got:
Jan 17, 2019
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
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.
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.
Edit: 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.
来源:https://stackoverflow.com/questions/54228178/how-to-localise-date-format-automatically-in-java-8