code_junkie

Customizing java.text formatters for different Locales

Nov 11th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. Settings formats = new Settings();
  2. Properties SDFFormats = formats.load(propertiesFile);
  3.  
  4. String SDFAmerica = SDFFormats.getProperty("FormatAmerica");
  5.  
  6. FormatAmerica = MMM-dd-yyyy
  7.  
  8. // This is an incredibly ugly hack, but it's based on the fact that
  9. // Java for some reason decided that Italy uses "." between
  10. // hours.minutes.seconds, even though "locale" and strftime say
  11. // something different.
  12. hmsTimeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
  13. if (hmsTimeFormat instanceof SimpleDateFormat)
  14. {
  15. String str = ((SimpleDateFormat)hmsTimeFormat).toPattern();
  16. str = str.replace('.', ':');
  17. hmsTimeFormat = new SimpleDateFormat(str);
  18. }
  19.  
  20. SimpleDateFormat sdf = (SimpleDateFormat)sdf.getDateTimeInstance(DateTime.SHORT,DateTime.SHORT, locale);
  21. if (formatString != null) {
  22. sdf = new SimpleDateFormat(formatString);
  23. }
  24. if (am!= null && pm != null) {
  25. DateFormatSymbols symbols = sdf.getDateFormatSymbols();
  26. symbols.setAmPmStrings(new String[]{am, pm});
  27. sdf.setDateFormatSymbols(symbols);
  28. }
Add Comment
Please, Sign In to add comment