0
votes

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).

1
It's unclear whether you're actually talking about the Gregorian calendar or the ISO textual format. I've just tried 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 Skeet
I have edited the question in order to provide you with more information. - user3245747
Well this sounds like it's about SQL rather than anything else then - and you haven't told us anything about the database or what's stored in it... - Jon Skeet
Its not about sql. The year1 variable has a value of 26 when it should have a value of 2014. This is why the sql query is returning an empty dataset; no records satisfy the condition of having a year of 26. The year1 variable is generated in the java code after I set the locale. - user3245747
By the way, calling Locale.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

1 Answers

2
votes

You can just check whether Calendar.getInstance(Locale) returns a GregorianCalendar:

for (Locale locale : Calendar.getAvailableLocales()) {
  if (locale.getCountry().length() > 0
      && Calendar.getInstance(locale) instanceof GregorianCalendar) {
    addItem(locale.getDisplayName());
  }
}