2

I am planning to write generalized method to support all date formats. can u please give me how to handle different kind of date formats. especially DD/MM/YYYY and MM/DD/YYYY.

And tell me date formats can be specific to locale wise? IF yes where to get date formats based on locale wise?

skaffman
  • 398,947
  • 96
  • 818
  • 769
pradeep cs
  • 513
  • 4
  • 9
  • 34
  • check this http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat – Vaandu Jan 25 '12 at 08:55
  • vanathi, if we dont know date format. based on date if we need to find out, how can we do that? as per your link, simple date format will support only if we know the date format. If user changes dates in any kind of format, how can we support from our end? – pradeep cs Jan 25 '12 at 09:06
  • the DD/MM/YYYY in the simple date format declaration is the input date format and not how we want to see the date... – Vicky Jan 25 '12 at 09:33
  • As far as we know, unless we know the format, we can't support it. We shall give them choice to select date format (for which we support). – Vaandu Jan 25 '12 at 09:35
  • 3
    @poineer: if your question is: "how to parse a date without knowing its format?", it's impossible. There is no way to know if 01/02/2012 if 1st Feb. 2012 or 2 jan. 2012. – JB Nizet Jan 25 '12 at 09:50

1 Answers1

2

The DateFormat factory methods can take a Locale as a parameter. The formatter will then parse date strings according to the format for that language/country.

For example:

Locale myLocale = Locale.US;
SimpleDateFormat sdf = SimpleDateFormat.getDateInstance(DateFormat.SHORT, myLocale);
Date d = sdf.parse("03/10/2012");

Will return a date representing the 10th of March 2012. But if myLocale = Locale.UK, it would represent the 3rd of October 2012.

The various locales can be enumerated by calling DateFormat.getAvailableLocales(), or the Locale constructors will take specific language or country codes.

GenericJon
  • 8,746
  • 4
  • 39
  • 50