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