Localizing dates

C

Chris

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

Bob

Chris said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top