When Google you will find lot of materials to find all the supported Locales by Java. But its all confusing.
For example [http://sanjaal.com/java/tag/java-locale-tutorial/] show an output of 210 locales. But when I run the same program I get only 155. I don;t get for example ta_IN. si_LK is not output by any program.
Can someone please clear the air?
I use JDK/JRE 1.7
http://www.oracle.com/technetwork/java/javase/javase7locales-334809.html gives 111 entries.
I have a Spring Application which supports i18n and our customers can put their own localisations. What I am trying to do is to provide a list of all locales for them to select their one from.
Oh! this is confusing. Local.getISOCountries() provide LK as a country and Locale.getISOLanguages(); provide si as a language .... but si_LK which is a valid locale is not given in Locale.getAvailableLocales();
Locale loc = new Locale("ta", "IN"); // ta_IN, si_LK
System.out.printf("Name: %s%n"
+ "Country: %s; %s - %s%n"
+ "Language: %s; %s - %s%n"
+ "Script: %s - %s%n",
loc.getDisplayName(Locale.ENGLISH),
loc.getCountry(), loc.getISO3Country(), loc.getDisplayCountry(Locale.ENGLISH),
loc.getLanguage(), loc.getISO3Language(), loc.getDisplayLanguage(Locale.ENGLISH),
loc.getScript(), loc.getDisplayScript(Locale.ENGLISH));
Name: Tamil (India)
Country: IN; IND - India
Language: ta; tam - Tamil
Script: -
Name: Sinhalese (Sri Lanka)
Country: LK; LKA - Sri Lanka
Language: si; sin - Sinhalese
Script: -
Also it is possible to provide support for ones own locale (since Java 7 I believe). I made it for Esperanto, and it is doable (LocaleProvider). But in your case all might be there.
SimpleDateFormat f = new SimpleDateFormat("EEEE", loc);
System.out.println("Weekday: " + f.format(new Date()));
Unfortunately shows "Tuesday," so one needs to implement the language formats and such.
I found a project for serbo-croatian/bosnian.
The code you found on internet use the next function to extract all the locales:
Locale list[] = DateFormat.getAvailableLocales();
If we read the documentation for that function:
Returns an array of all locales for which the get*Instance methods of this class can return localized instances. The returned array represents the union of locales supported by the Java runtime and by installed DateFormatProvider implementations. It must contain at least a Locale instance equal to Locale.US
So we can understand that the number of locales returned depends on your runtime version, and your DateFormatProvider.
For example in my machine with JDK 1.6.0_27 I get 152 results.
To provide a list of all ISO countries in your JAVA Version try the following:
public static void main(String[] args) {
String[] locales = Locale.getISOCountries();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
System.out.println("Country Code = " + obj.getCountry()
+ ", Country Name = " + obj.getDisplayCountry());
}
}
This way you can provide a complete list of all countries but it is still dependend on the current ISO country list of your JDK.
Another way would be to use a REST-API from a provider like: https://restcountries.eu/rest/v2/all from https://restcountries.eu/
This way your program is independent of the JDK you use and is always up2date (if your chosen provider is reliable).
Related
I know of three JFR Event types: Instant Event, Duration Event, and Sample Event, but how to identify JFR event types.
I try to distinguish them from the configuration, but it seems doesn't work, for example, jdk.ObjectAllocationInNewTLAB, it only need to configure whether to enable, seems to be an Instant Event, but is actually a Sample of the Event.
This is important to me because I want to analyze with full information, not samples
You can see what options an event type supports by using the setting descriptor:
for(EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) {
System.out.println(type.getName());
System.out.println("Settings:");
for (SettingDescriptor s : type.getSettingDescriptors()) {
String def = " (default: " + s.getDefaultValue() + ")";
System.out.println(" " + s.getName() + def);
}
System.out.println();
}
It's also possible to list event metadata, including settings, using the 'jfr' tool located in JAVA_HOME/bin. For JDK 11, you must supply the file you want metadata to be printed for:
$ jfr metadata recording.jfr
For JDK 17, you can omit the file and you will get the event types for the JDK the tool is located in:
$ jfr metadata
jdk used : 1.8
Not sure what is the issue, configuredFormat is valid one, inputTime is also valid one, really confused what is the issue.
public class Test {
public static void main(String[] args) {
String configuredFormat = "yyyyMMddHHmmssSSS";
String inputTime = "20200203164553123";
DateTimeFormatter dt = DateTimeFormatter.ofPattern(configuredFormat);
DateTimeFormatter strictTimeFormatter = dt.withResolverStyle(ResolverStyle.STRICT);
try {
LocalTime.parse(inputTime, strictTimeFormatter);
System.out.println("success");
} catch (DateTimeParseException | NullPointerException e) {
e.printStackTrace();
}
}
}
Exception I am Getting :
java.time.format.DateTimeParseException: Text '20200203164553123' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalTime.parse(LocalTime.java:441)
at com.Test.main(Test.java:20)
Lucky for you, there is an exact bug report which uses the exact same pattern that you're trying. Who better to explain than the JDK maintainers?
JDK-8031085
Workaround
DateTimeFormatter dtf = new
DateTimeFormatterBuilder()
.appendPattern("yyyyMMddHHmmss")
.appendValue(ChronoField.MILLI_OF_SECOND,3)
.toFormatter()
Adjacent value parsing is generally a hard problem. It is intended to
handle the case where the first element is variable width (the year)
and all other elements are fixed width (month, day etc). However, the
"S" pattern letter is a fraction, not a value. Specifically, the
fraction can be variable width - more or less than three digits are
possible options. Given the general case of a variable width year and
a variable width millisecond, it is not possible to determine which of
the two fields was intended to be variable.
Having said that, the implementation (and javadoc) have not ended up
as I intended. The description of "fraction" in DateTimeFormatter
describes actions in strict and lenient mode, but there is no way to
access strict or lenient mode when using
DateTimeFormatter.ofPattern(). This is a documentation bug that should
be fixed by removing the discussion of strict vs lenient.
Worse however is that the SSS pattern has therefore ended up using
strict mode when lenient mode would be appropriate. As it currently
stands, DateTimeFormatter.ofPattern("hhmmss.SSS") requires three
digits for milliseconds, when it was originally intended to require 0
to 9 (the lenient behaviour).
I tried changing the whole of the DateTimeFormatter.ofPattern() method
to use lenient parsing, and it broke no tests (which is bad in its own
way). This might be a valid fix, but only if included in JDK 8, as
once people adapt to the strict parsing it will be hard to make it
lenient.
Given that the current implementation requires three digits for SSS,
it is thus very surprising that adjacent value parsing does not apply.
Actually I agree format should be valid... this seems to be confirmed as I tried with both oracle java 8 and 9 runtime, and with java 9 it does not happen. (I tried IBM jre 8 too and it works as well)
System.out.println( System.getProperty( "java.vendor" )+" - "+System.getProperty( "java.version" ) );
String configuredFormat = "yyyyMMddHHmmssSSS";
String inputTime = "20200203164553123";
DateTimeFormatter dt = DateTimeFormatter.ofPattern(configuredFormat);
DateTimeFormatter strictTimeFormatter = dt.withResolverStyle(ResolverStyle.STRICT);
try {
//System.out.println( dt.parse( inputTime ) );
LocalTime.parse(inputTime, strictTimeFormatter);
System.out.println("success");
} catch (DateTimeParseException | NullPointerException e) {
e.printStackTrace();
}
Output
Oracle Corporation - 9.0.4
success
IBM Corporation - 1.8.0_211
success
Oracle Corporation - 1.8.0_172
java.time.format.DateTimeParseException: Text '20200203164553123' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalTime.parse(Unknown Source)
at test.Test2.main(Test2.java:19)
Would need on identifying what is the locale id for Austria. I have tried with de_AT/at/at_at as mentioned here
But it seems Locale API doesn't recognize these locale, any pointers..?
Exception when trying with de_AT:
java.util.MissingResourceException: Couldn't find 3-letter language code for de_at
at java.util.Locale.getISO3Language(Unknown Source)
at com.test.rupesh.CQInstanceHelper.language(Main.java:74)
at com.test.rupesh.Main.main(Main.java:20)
What are you trying with de_AT exactly?
Locale austria = new Locale("de", "AT");
System.out.println("Locale=" + austria);
System.out.println("ISO3 language=" + austria.getISO3Language());
prints out:
Locale=de_AT
ISO3 language=deu
EDIT: Do not instantiate locales this way:
Locale austria = new Locale("de_AT");
System.out.println("Locale=" + austria);
System.out.println("ISO3 language=" + austria.getISO3Language());
prints out:
Locale=de_at
Exception in thread "main" java.util.MissingResourceException: Couldn't find 3-letter language code for de_at
at java.util.Locale.getISO3Language(Locale.java:568)
at Scribble.main(Test.java:10)
check the available locations using this
System.out.println(Arrays.asList(Locale.getAvailableLocales()));
The following snippet behaves different in Java 6 than Java 7:
final Locale locale = new Locale("nb", "NO");
System.out.println(locale.getDisplayLanguage()); // Norwegian Bokmål
final DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale);
System.out.println(dfs.getDecimalSeparator()); // Java 6: .
// Java 7: ,
Why is that? Is this change documented somewhere?
Java 6 had a number of issues regarding Locales, and this may well be one of them. Certainly, the correct separator for the Norway locale is ,.
The Oracle bug database does show quite a few bugs related to NO locale...
According to JDK 6 and JRE 6 Supported Locales and JDK 7 and JRE 7 Supported Locales, the correct/supported syntax for selecting Norwegian Bokmål is "no"/"NO".
new Locale("no", "NO") gives the correct result under both Java 6 and Java 7.
CLDR
In Java 9 and later, the OpenJDK-based implementations of Java switched to using locale data obtained from the Unicode Consortium’s Common Locale Data Repository (CLDR) (see Wikipedia) by default.
This switch provides much richer locale data than previously seen in Java. And this switch may mean that you see a change in behavior for some aspects of some locales.
See: JEP 252: Use CLDR Locale Data by Default
COMMA in Java 10
When I run this code:
System.out.println( "java.version: " + System.getProperty( "java.version" ) );
final Locale locale = new Locale( "nb" , "NO" );
System.out.println( locale.getDisplayLanguage() ); // Norwegian Bokmål
final DecimalFormatSymbols dfs = new DecimalFormatSymbols( locale );
System.out.println( dfs.getDecimalSeparator() );
I get the COMMA character.
java.version: 10.0.2
Norwegian Bokmål
,
I'm trying to understand more about Java's MessageFormat utilities, and in examples in our codebase and elsewhere I see both {0} and {0,number,integer} being used for numbers, but I'm not sure which, if either, is preferable.
A quick test printing the differences:
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class MessageFormatTest
{
public static void main(String[] args){
MessageFormat simpleChoiceTest = new MessageFormat("{0}");
MessageFormat explicitChoiceTest = new MessageFormat("{0,number,integer}");
int[] set = new int[]{0,1,4,5,6,10,10000,24345};
Locale[] locs = new Locale[]{Locale.US,Locale.UK,Locale.FRANCE,Locale.GERMANY};
for(Locale loc : locs){
simpleChoiceTest.setLocale(loc);
explicitChoiceTest.setLocale(loc);
for(int i : set){
String simple = simpleChoiceTest.format(new Object[]{i});
String explicit = explicitChoiceTest.format(new Object[]{i});
if(!simple.equals(explicit)){
System.out.println(loc+" - "+i+":\t"+simple+
"\t"+NumberFormat.getInstance(loc).format(i));
System.out.println(loc+" - "+i+":\t"+explicit+
"\t"+NumberFormat.getIntegerInstance(loc).format(i));
}
}
}
}
}
Outputs:
fr_FR - 10000: 10 000 10 000
fr_FR - 10000: 10,000 10 000
fr_FR - 24345: 24 345 24 345
fr_FR - 24345: 24,345 24 345
de_DE - 10000: 10.000 10.000
de_DE - 10000: 10,000 10.000
de_DE - 24345: 24.345 24.345
de_DE - 24345: 24,345 24.345
Which surprised me, if anything I would have expected the {0} to not do anything to the number, and for {0,number,integer} to localize it properly. Instead, both get localized, but it seems the explicit form always uses en_US localization.
According to the linked documentation, {0} gets put through NumberFormat.getInstance(getLocale()) while while the explicit form uses NumberFormat.getIntegerInstance(getLocale()). Yet when I call those directly (the last column in the output) both seem identical, and both localize correctly.
What am I missing here?
You are right. When you use "MessageFormat("{0,number,integer}")", formatter uses default locale(en_US) at the time of initialization and numbers are marked to use Integer format in default locale(en_US) as the code below is executed during the initialization time itself.
// this method is internally called at the time of initialization
MessageFormat.makeFormat()
// line below uses default locale if locale is not
// supplied at initialization (constructor argument)
newFormat = NumberFormat.getIntegerInstance(locale);
Since you are setting the locale afterwards, there is no impact on the format pattern assigned to numbers. If you want to use the desire locale in the format for numbers, please use the locale argument at the time of initialization itself e.g. below:
MessageFormat test = new MessageFormat("{0,number,integer}", Locale.FRANCE);
In my opinion, this is a Java bug (the interface is wrong) or a documentation problem. You should open a new issue at Oracle to correct that.
As Yogendra Singh, said the instance of the formatter (DecimalFormat) is created when the MessageFormat constructor.
MessageFormat simpleChoiceTest = new MessageFormat("{0}");
System.out.println(simpleChoiceTest.getFormatsByArgumentIndex()[0]);
//Prints null
MessageFormat explicitChoiceTest = new MessageFormat("{0,number,currency}");
System.out.println(explicitChoiceTest.getFormatsByArgumentIndex()[0]);
//Prints java.text.DecimalFormat#67500
When the MessageFormat.setLocale is called it does not change the locale of its internal formatters.
At least the documentation should be changed to reflect this issue.
That is my java version:
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b11)