Java Regional Settings Problem

M

memzer

Hi,

I am having difficulties formatting dates for logging using the
DateFormat object in Java (1.5.0_06). It seems that Java is ignoring
any changes I make to my workstations Regional Settings (Windows XP,
Service Pack 2) when it formats the dates for display. To demonstrate I
generated a quick test application and tested date formatting for two
sets of Regional Settings - the default settings for en_AU, and my
custom settings (I have included the test applications source, output
of the 'samples' section of the Regional Settings dialog in Windows
XP, and the output of the program after being run with the appropriate
regional settings). As you can see from the output, it doesn't appear
that the DateFormat object is being updated to reflect the current
systems Regional Settings.

Does anybody else experience this? Is this a bug with Java? Regardless,
if anybody does know a method of getting the user defined date/time
formatting patterns from the operating system could they please let me
know?!!

Thanks for your time.
(I apologise about the length of this message)

Default Regional Settings for English, Australia
------------------------------------------------

Number: 123,456,789.00
Currency: $123,456,789.00
Time: 1:02:47 PM
Short date: 26/03/2006
Long date: Sunday, 26 March 2006

Custom Regional Settings for English, Australia
-----------------------------------------------
Number: 123456789.00
Currency: $123,456,789.00
Time: 12:59:39
Short date: 2006/03/26
Long date: Sunday, 26 March 2006

Test Source Code
----------------

import java.text.*;
import java.util.*;

public class DateTest
{
public static void main( String[] args )
{
Date date = new Date();
DateFormat dfDefault = DateFormat.getDateInstance();
DateFormat dfShort = DateFormat.getDateInstance(
DateFormat.SHORT );
DateFormat dfMedium = DateFormat.getDateInstance(
DateFormat.MEDIUM );
DateFormat dfLong = DateFormat.getDateInstance( DateFormat.LONG
);
DateFormat dfFull = DateFormat.getDateInstance( DateFormat.FULL
);

System.out.println("Default: " + dfDefault.format(date));
System.out.println("Short: " + dfShort.format(date));
System.out.println("Medium: " + dfMedium.format(date));
System.out.println("Long: " + dfLong.format(date));
System.out.println("Full: " + dfFull.format(date));

SimpleDateFormat format = new SimpleDateFormat();
System.out.println("Pattern: " + format.toPattern());
}
}

Output (using Default Regional Settings)
-----------------------------------
Default: 26/03/2006
Short: 26/03/06
Medium: 26/03/2006
Long: 26 March 2006
Full: Sunday, 26 March 2006
Pattern: d/MM/yy HH:mm

Output (using Custom Regional Settings)
----------------------------------
Default: 26/03/2006
Short: 26/03/06
Medium: 26/03/2006
Long: 26 March 2006
Full: Sunday, 26 March 2006
Pattern: d/MM/yy HH:mm
 
R

Roedy Green

I am having difficulties formatting dates for logging

my thinking is you should use iso format dates, especially for
something like a log. They have the form yyyy-mm-dd

In Canada there are at least six formats in common use including

yyyy-mm-dd - iso
yyyy.mm.dd - federal government
mm/dd/yyyy
dd/mm/yyyy
dd-mm-yyyy
mm-dd-yyyy

At least iso is unambiguous since nobody is pushing yyyy-dd-mm yet.
 
J

jasonwea

Hi there,

On my OS X 10.4.5 machine running 1.5.0_06 I get somewhat disappointing
output. My regional settings are based on English Australia but with
the date format set to yyyy-mm-dd. I also tried setting back to default
English Australia but with the same results:

Default: Mar 26, 2006
Short: 3/26/06
Medium: Mar 26, 2006
Long: March 26, 2006
Full: Sunday, March 26, 2006
Pattern: M/d/yy h:mm a

It seems to be always using en-us. Not good when you want to localise
your date outputs. Does anyone have any ideas on this?

Regards,

Jason W
 
M

memzer

Thanks for the reply,

I personally agree with you - date and times in log files should be
stored in compliance with an appropriate standard. Our application
allows date and time to be stored in log files in accordance with ISO
8601, in a format which matches a default locale (e.g. en_AU, en_US,
etc), or based upon a user defined date and time pattern.

But I digress, the problem we are having is actually to do with the
display of log files. Our application provides users with a GUI to
peruse any logs they have generated. I believe that when displaying
date and time information from a log file (regardless of its storage
format) the saved information should be formatted for in accordance
with the user's preference for date and time formatting. And this is
where the problem is.

Java seems to ignore the user defined date formatting preferences
(Defined in Regional Settings for Windows XP). As in my original post,
my workstation preferences for the formatting of short dates is
"yyyy/mm/dd" - customised en_AU regional settings profile. When
formatting dates however, Java seems to just use the default settings
for the en_AU profile ("dd/mm/yyyy").

If you have any idea of how to get the customised pattern preference
data I would appreciate it if you could post an example.

Anyway, thanks again for the reply!!
 
R

Roedy Green

time to be stored in log files in accordance with ISO
8601, in a format which matches a default locale (e.g. en_AU, en_US,
etc), or based upon a user defined date and time pattern.

If the log files will be processed in some other locale, or if they
will ever be merged, you will wish you had gone for a
locale-independent format yyyy-mm-dd
 
J

jasonwea

Hi Roedy,

I think you may have missed memzer's point. From what I can gather he
is storing his dates in a portable fashion.

We both are trying to format a java.util.Date for display purposes in
the user's preferred format.

On my OS X workstation this is proving to be fairly futile as I am
always getting en-us format. memzer seems to be getting a standard
en-au format without user customizations. It's like the JRE has it's
own list of formats for each locale and does not consult the OS.

Perhaps we'll both have to use JNI to get what we want? :/

Kind regards,

Jason W
 
M

memzer

Hi Jason,

I have been looking into this some more and it definitely appears that
there is no way to get system derived formatting patterns from Java.
Some research produced the following posts of users who are trying to
accomplish essentially the same thing I am:

http://forum.java.sun.com/thread.jspa?forumID=16&threadID=318720
http://forum.java.sun.com/thread.jspa?forumID=16&threadID=244377
http://forum.java.sun.com/thread.jspa?forumID=16&threadID=544738
http://72.14.203.104/search?q=cache...date+format+pattern"&hl=en&gl=au&ct=clnk&cd=1
( from Google's cache )

I guess that like you suggested, a JNI solution may be the only way to
go at this point. If anybody knows of an open library that can extract
these formatting patterns from Windows (98 to XP), Linux (KDE/Gnome)
and Mac OS X - or some appropriate resources for doing so - I would
appreciate it if you could please post a reference here.
 
M

memzer

Hi Roedy,

At this point I have decided that I will provide a display preferences
panel and allow a user to format output in accordance with either one
of the Java locales, or as a user defined formatting pattern. When I
get some more time I will work on using JNI to get the users system
preferences. Mind you, it seems a shame to have to use JNI for
something like this, given Java's reputation for easily creating
internationalized applications...
 
J

jasonwea

I've been doing some searching and haven't found any existing libraries
for Java. Looks like it's time to roll our own.

strftime <http://www.icewalkers.com/Linux/ManPages/strftime-3.html>
should be fairly portable. GetDateFormat
<http://www.microsoft.com/globaldev/getWR/steps/wrg_date.mspx> is
available on Windows and has a nice API. It should be the best way to
do things on Windows as it's sure to get the formatting how we want.

Since my application already has a JNI library for other purposes, I'll
just be adding a new function to it. It will take a java.util.Date and
an enum specifying the format desired. It will return a String
containing the formatted date.

I wonder if this would be a useful project for JDesktop Integration
Components <https://jdic.dev.java.net/>.

Jason
 
Joined
Jul 26, 2006
Messages
1
Reaction score
0
Tomcat includes such a class already

Java Doc

Source Code

Basically it just converts the strftime we all know and love into Java's random format :smile: .

I wonder if something like this could get into Java SE 7 (Dolphin)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top