I have a java program where the user can choose the locale which he wants from a checkbox. in order to populate this checkbox I use the Calendar.getAvailableLocales()
method. The problem is that this method returns locales which use dates which are not Gregorian and this causes problems in my program which only supports Gregorian dates (ISO). Two examples are the Japanese (Japan, jP) locale and the Thai (Thailand, TH) locale. I do not want these types of locales to appear in the program. How can I filter the list such that only locales with ISO calendar formats are available in the checkbox? Thanks.
EDIT:
Ok. Here is the code for the JLocaleChooser
combo box that I use:
locales = Calendar.getAvailableLocales();
localeCount = locales.length;
for (int i = 0; i < localeCount; i++) {
if (locales[i].getCountry().length() > 0) {
addItem(locales[i].getDisplayName());
}
}
After that I set the locale of the system based on the user choice as such:
myLocale2 = theJLocaleChooser.getLocale();
Locale.setDefault(myLocale2);
If the user chooses the Japanese (Japan) locale then everything is fine. if he/she chooses the Japanese (Japan, JP) locale my program does not perform operations like it should. For example, somewhere in the program I get the current month and use the information in a sql statement as such:
Calendar cal0 = Calendar.getInstance();
int month1 = cal0.get(Calendar.MONTH);
month1++; //since 0 is january
int year1 = cal0.get(Calendar.YEAR);
connect.query = "SELECT name1, name2 FROM table1 WHERE MONTH(date) = " + month1 + " AND YEAR(date) = " + year1;
This sql query returns an empty dataset if the user chose Japanese(Japan, JP) but returns the correct dataset if the user chose the Japanese(Japan) locale. The reason is that the value of the year1 variable if I use the Japanese (Japan, JP) locale is 26 (I used a println
command) but it is 2014 (like it should be) if I use other locales including the Japanese (Japan) one. Therefore I need locales that use the "regular" years (2012, 2013,...) not other years (like the Islamic Hijra for example).
Calendar.getInstance(Locale.JAPAN)
and it's still returned a Gregorian calendar - can you say exactly what you mean by "not supporting" Gregorian dates? - Jon SkeetLocale.setDefault
is not usually recommended. That command affects all code running in all threads in all apps running in that JVM even as they are executing. Better to store the desired Locale object, and use it to specify wherever a Locale is needed. - Basil Bourque