Localizing dates

Discussion in 'Java' started by Chris, Jan 28, 2005.

  1. Chris

    Chris Guest

    In the US, the most common date format is mm/dd/yyyy. In Europe, it's
    dd/mm/yyyy.

    Is there any way to know which format to use, based on the Locale? It
    doesn't appear that java.text.DateFormat or java.text.DateFormatSymbols know
    about or acknowledge the difference.

    I need to know because I have to parse dates in different locations, and I
    want to give the parser a hint about what format to expect. Specifying a
    format explicitly at run time is not a possibility.
     
    Chris, Jan 28, 2005
    #1
    1. Advertising

  2. Chris

    Bob Guest

    Chris wrote:
    > In the US, the most common date format is mm/dd/yyyy. In Europe, it's
    > dd/mm/yyyy.
    >
    > Is there any way to know which format to use, based on the Locale? It
    > doesn't appear that java.text.DateFormat or java.text.DateFormatSymbols know
    > about or acknowledge the difference.


    You can get the default locale of the JVM your code is running on by calling

    Locale currentLocale = Locale.getDefault()

    which will return something like "en_GB" or "en_US" if you convert it to
    a String representation.

    You can create a DateFormate object for a non-default locale by
    specifying one when you create a DateFormat object, like so:

    Locale EN_GB = new Locale("en", "US");
    DateFormat df
    = DateFormat.getDateInstance(DateFormat.SHORT,
    EN_GB);
    Date dateThisYear;
    try {
    dateThisYear = df.parse("12/01/2005");
    } catch (Exception pe) {
    System.out.println("Caught ParseException: " + pe);
    dateThisYear = null;
    }

    System.out.println(dateThisYear);

    > I need to know because I have to parse dates in different locations, and I
    > want to give the parser a hint about what format to expect. Specifying a
    > format explicitly at run time is not a possibility.


    This is the problem with not using standards. I wish everyone could
    agree to use the format

    YYYY/MM/DD HH:MM:SS (always with four-digit year)

    Highest-to-lowest, like the Swedes use (and probably plenty of other
    nations). Then there might be no confusion.
    --
    Bob
     
    Bob, Jan 29, 2005
    #2
    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. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    896
    Rob Schieber
    Sep 30, 2005
  2. Chris

    Localizing dates

    Chris, Jan 28, 2005, in forum: Java
    Replies:
    9
    Views:
    445
    Tony Dahlman
    Feb 22, 2005
  3. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    223
    Mark Schupp
    Aug 9, 2004
  4. Replies:
    1
    Views:
    229
    Jano Svitok
    Jul 17, 2007
  5. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    233
    Eric I.
    Jun 8, 2008
Loading...

Share This Page