I have a piece of code like this on my java side:
private static DateFormat getHourFormatter(){
//DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(_locale);
Locale locale = Locale.FRENCH; //locale : "fr"
DateFormat hourFormatter = new SimpleDateFormat( "hh:mm a",locale); //hourFormatter: simpleDateFormat@103068 locale: "fr"
hourFormatter.setTimeZone( TimeZone.getTimeZone("GMT") );
return hourFormatter; //hourFormatter: SimpleDateFormat@103068
}
protected static boolean isHoursTimeStringValid( String hourDisplay ) {
try {
getHourFormatter().parse( hourDisplay ); //hourDisplay: "01:01 Matin"
return true;
} catch (ParseException e) { //e: "java.text.ParseException: Upparseable date "01:01 Matin"
return false;
}
}
It is working fine for English locale if I change the locale value to US.
But for French locale it throwing parsing error.
java.text.ParseException: Upparseable date "01:01 Matin"
I have added the debug info as commented line for better understanding
Thank you guys for all of these answers.
As I mentioned earlier, I can't afford to change the code base.
So, what I have done is :
public void setBeginAMPM( String ampm ) {
if(ampm.equals(new I18NStringFactory().getString("Calendar", _locale , "default.time.am" ))) {
_beginAMPM = "AM";
}
else if(ampm.equals(new I18NStringFactory().getString("Calendar", _locale , "default.time.pm" ))) {
_beginAMPM = "PM";
}
else{
_beginAMPM = ampm;
}
}
public void setEndAMPM( String ampm ) {
if(ampm.equals(new I18NStringFactory().getString("Calendar", _locale , "default.time.am" ))) {
_endAMPM = "AM";
}
else if(ampm.equals(new I18NStringFactory().getString("Calendar", _locale , "default.time.pm" ))) {
_endAMPM = "PM";
}
else{
_endAMPM = ampm;
}
}
_locale value I am passing from Action class to From class. If it is other than English it will come into one of the if block or in case of English it will come to the else block by default. Based on the local value it is taking AM/PM value from the properties file and converting that accordingly.
I am just modifying the AM/PM value from other locale-specific languages to English, as SimpleDateFormat() only supports English.
You guys can call it an ugly hack, but guess what, It is solving my purpose.