views:

50

answers:

2

I'm writing a java gui application that has to display dates.

Since this application is primarily going to run on Windows systems, I would like to be able to use date & time formats that correspond to the Windows localization settings.

I found DateFormatProvider class, in Java 6, which gave me high hopes ... but I haven't found an implementation that will use the Windows localization information.

Any suggestions?

A: 

You should be able to use the DateFormatProvider methods with the default locale returned by getDefault().

The Java Virtual Machine sets the default locale during startup based on the host environment.

EDIT: if you can't just pass the default locale to the DateFormat class, there is example code here for implementation of a concrete class that extends DateFormatProvider.

Steve Townsend
The DateFormatProvider is abstract ... how do I create one that will use the Windows formatting?
david
@david - see edit, does that help?
Steve Townsend
I saw that concrete implementation elsewhere (or something very similar) ... and I understand that's how you implement it, but I want something that will use the Windows localization information to create the formats. So far I haven't found thing that will. I suspect there are Windows API calls I could make to get the formatting information, but I don't want to start messing with JNI stuff.
david
@david - I suggest you confirm how the JVM sets up the default locale. This could be using the APIs you reference. Then you'd just need to produce a `DateFormatProviderImpl` which uses the default locale. Alternatively, you could also automate override of the default locale config in your JVM options using Win32 information, as described here: http://stackoverflow.com/questions/1153521/setting-default-locale-for-tomcat-service-in-windows-xp
Steve Townsend
@Steve, all my experimentation has shown that Java doesn't use the windows localization information at all. I expected Java to use the host OS's localization data, but it doesn't appear to be so.I was trying to use the `DateFormat.getDateTimeInstance(int,int,Locale)`, but it doesn't appear to be effected by Windows settings.
david
@david - in C# you can get the system culture using `CultureInfo.CurrentCulture`. My thought was that you could use this to customize JVM config options, such that `getDefault()` in `DateFormatProviderImpl/DateFormat` would act as you wish.
Steve Townsend
@david - `CultureInfo.InstalledUICulture` might be a better choice here since you are concerned about UI effects, I imagine. The underlying Win32 API if you want to avoid C# is `GetSystemDefaultUILanguage`.
Steve Townsend
@Steve JNI is really not an option.
david
@david - I am not suggesting JNI, I am suggesting you programmatically modify your JVM config options from C# or C++ (which can be file-based, if I am not mistaken) before your app starts up. The intent is to ensure the JVM starts up in such a state that you can use `getDefault()` as a Java `Locale` to achieve what you want.
Steve Townsend
@Steve that's more work than this particular problem is worth. If there isn't a SPI provider for Windows localization, I'll just use the default formatting and live with it.
david
@david - it's hard to imagine how such an animal exists out of the box without JNI, yes? All the best with solving this, anyway.
Steve Townsend
A: 

I haven't heard about any Java date formatter that uses MS Windows formatting routines (or just definitions for that matter). Since Java is meant to be multiplatform (compile once, run anywhere), it simply couldn't use underlying OS behavior, for consistency reasons.

You can use DateFormat class as defined here:

DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
String formattedDate = dateFormatter(new Date());

One important issue about this class: LONG date formats are broken for Czech, Polish and Russian, possibly for other Slavic languages too. I recommend using MEDIUM date format instead.

BTW. This will format dates for Gregorian or Julian calendars. It won't give you Arabic nor Hebrew calendars (although former is default for some countries).


Edit

I am not aware of your specific requirements, but since you mentioned Locale, maybe ICU4J's DateFormat class is what you looking for. Still, as per my knowledge (which might be incomplete here), they are using their own formatter and localized text database. However, this database is probably more complete (especially on Mac) than the one bundled with JDK (previous ICU snapshot).

Paweł Dyda
From what I have read, the DateFormatProvider class is intended to provide this kind of service functionality. I just can't find an implementation that does it.
david
DateFormatProvider is an abstract base class and DateFormat is its implementation, as stated in JavaDoc you gave link to. It does not use Windows formatting, it uses built-in formatting instead.
Paweł Dyda
@Pawel, yes, that's what I observed. This is why I'm looking for an implementation of `DateFormatProvider` that will use the Windows localization information.
david
@David: Well, good luck. I thought, I gave you good reason why such class should not exist, but turns out I did not. Of course you can use JNI/JNA to create such binding yourself, but I don't quite think it is worth the effort.
Paweł Dyda
@Pawel - I understood your comment about Java being platform neutral ... but the JVM implementation for each platform is specific ... so I would expect that each JVM implementation would have some way to access that platforms localization information.As such, a Windows JVM should have the ability to format dates according to the Windows localization set in the control panel. Similarly, a Linux implementation should have the ability to format dates using it's localization information. If there is no localization information, the JVM would default to generic info.
david
@David: I understand. Unfortunately, due to different Locale models on various Operating Systems, it probably wasn't so easy to do. I am not even sure if all supported OS's even have date/time or number formatters. Still, it could be done for Windows only, but the problem is, the results would be inconsistent.
Paweł Dyda
@Pawel - I don't know ... I think most OS's have localization information. I'm pretty sure Linux does ... and Mac has it also. It all depends on the windowing system. The goal is for Java apps to be consistent with the host OS. This is, of course, all moot ... since the java SPI doesn't have an implementation specific for Windows.
david