3

I am facing issue with Date.parse() method for non-English locale with English OS. I am using the Date.parse() method to parse the entered value of date. It would be different for different locales. Here are some examples. 03-Feb-2016 00:00 English 01.Feb.2016 00:00 Germany 2016-2-01 00:00 Japanese

So for German locale, above string (01.Feb.2016 00:00) pass to the parse()

            try
            {
                Date.parse( value );
            }
            catch( IllegalArgumentException e )
            {
                valid_value= false;
            }

where value is "01.Feb.2016 00:00" then it goes to catch block. Please help me on how can I validate the value or convert the value acceptable by English locale.

3
  • 1
    you should have DateFormat.getDateInstance(int style, Locale locale) , you can refer to the documentation at docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html Commented Feb 4, 2016 at 13:21
  • SimpleDateFormat is usually the way to go for all your parsing needs... Commented Feb 4, 2016 at 13:22
  • Thanks for reply, Actually I went through that document and implemented same code but in my case (For German locale) it wasn't accepting the character "e" from "01.Feb.2016 00:00". Commented Feb 4, 2016 at 13:24

2 Answers 2

3

You can use following code

//Germany local date
Date date = new Date();
DateFormat dateFormat;
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,Locale.GERMANY);
dateFormat.format(date);

OR

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.GERMANY);
 simpleDateFormat.format(date);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the DateFormat.getInstance() to get a specific instance of formatter for your Locale:

DateFormat format = DateFormat.getInstance(SHORT, locale);

7 Comments

Hi in this case it returns the format as "dd.MM.YY" and not the "dd.mmm.YYYY" for German locale.
Well, unfortunately, you can choose only from the predefined ones if you do not want to define the format yourself. So SHORT, MEDIUM, LONG, FULL. If neither are what you are looking for, you might have to specify your "custom" format for each Locale you want to support.
@Jaydeep try MEDIUM instead of SHORT
maybe this can help new SimpleDateFormat("dd-MMM-yyyy", Locale.GERMANY)
@Vaseph It would be hard-coded for Germany. I wouldn't be bale to use for other locales.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.