Java Regional Settings Problem

Discussion in 'Java' started by memzer@gmail.com, Mar 26, 2006.

  1. Guest

    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
     
    , Mar 26, 2006
    #1
    1. Advertising

  2. Roedy Green Guest

    On 25 Mar 2006 19:37:13 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 26, 2006
    #2
    1. Advertising

  3. Guest

    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
     
    , Mar 26, 2006
    #3
  4. Guest

    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!!
     
    , Mar 26, 2006
    #4
  5. Roedy Green Guest

    On 26 Mar 2006 01:29:45 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 26, 2006
    #5
  6. Guest

    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
     
    , Mar 27, 2006
    #6
  7. Roedy Green Guest

    On 26 Mar 2006 22:42:26 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >It's like the JRE has it's
    >own list of formats for each locale and does not consult the OS.


    you could cook up your own locale objects.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 27, 2006
    #7
  8. Guest

    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.
     
    , Mar 27, 2006
    #8
  9. Guest

    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...
     
    , Mar 27, 2006
    #9
  10. Guest

    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
     
    , Mar 27, 2006
    #10
  11. worldofnic

    Joined:
    Jul 26, 2006
    Messages:
    1
    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)?
     
    worldofnic, Jul 26, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. René de Leeuw

    Regional settings

    René de Leeuw, Jul 2, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    586
    René de Leeuw
    Jul 2, 2003
  2. Lachlan James

    problem with regional & language settings

    Lachlan James, Aug 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    572
    Lachlan James
    Aug 5, 2003
  3. Phil
    Replies:
    0
    Views:
    614
  4. Karunakararao
    Replies:
    0
    Views:
    658
    Karunakararao
    May 11, 2004
  5. donet programmer
    Replies:
    3
    Views:
    1,581
    Gregory A. Beamer
    Nov 20, 2009
Loading...

Share This Page