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
Without rewriting your existing code base, you may still introduce
java.time
, the modern Java date and time API, for this particular purpose. It does offer a solution for French:This prints
Mixing
java.time
and the outdated classesSo far our code base (which is old) is a funny mix of old and new date and time API use. Frankly we’re seldom rewriting any of the old and working code, but we always use the modern API for new code.
I do warmly recommend using
java.time
wherever you can. It is generally so much nicer to work with. Once you have embarked on using it, I’m sure you will not want to go back.For a pure
SimpleDateFormat
solution see Meno Hochschild’s comment below.If and only if you have just two possible values (here AM/PM) then you can do it with
SimpleDateFormat
this way:I have here set the timezone to GMT in order to prevent any zone effects. You can deviate from it if needed (but care is necessary).
As mentioned in some comments, I still don't think that using the AM/PM-field is really appropriate for other languages than English. French for example knows at least two or more values like "nuit" (=night) or "après-midi" (=afternoon). But that way is not possible with old API or new
java.time
-package (would require external libs like ICU4J or Time4J).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 :
_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.