FormatUtility.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package commons.utility;
  2. import java.text.DecimalFormat;
  3. import java.text.DecimalFormatSymbols;
  4. import java.text.NumberFormat;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.List;
  9. import javax.xml.crypto.dsig.TransformException;
  10. import org.apache.commons.lang.StringUtils;
  11. import commons.utility.database.NullValue;
  12. /**
  13. * @author basluc
  14. *
  15. * Classe generica per le formattazioni
  16. */
  17. public final class FormatUtility
  18. {
  19. static public String formatText(String text)
  20. {
  21. return (null == text) ? "" : text;
  22. }
  23. static public String formatText(String text, int maxLength)
  24. {
  25. return (null == text) ? "" : text.length() <= maxLength ? text : text.substring(0,maxLength);
  26. }
  27. static public String formatValuta(int value)
  28. {
  29. DecimalFormatSymbols symbols = new DecimalFormatSymbols();
  30. symbols.setGroupingSeparator('.');
  31. symbols.setDecimalSeparator(',');
  32. DecimalFormat nf = new DecimalFormat("#,##0.00", symbols);
  33. return nf.format(value);
  34. }
  35. static public String formatValuta(double value)
  36. {
  37. DecimalFormatSymbols symbols = new DecimalFormatSymbols();
  38. symbols.setGroupingSeparator('.');
  39. symbols.setDecimalSeparator(',');
  40. DecimalFormat nf = new DecimalFormat("#,##0.00", symbols);
  41. return nf.format(value);
  42. }
  43. static public String formatValutaNoSeparator(double value)
  44. {
  45. DecimalFormatSymbols symbols = new DecimalFormatSymbols();
  46. symbols.setDecimalSeparator(',');
  47. symbols.setGroupingSeparator('.');
  48. DecimalFormat nf = new DecimalFormat("#,##0.00", symbols);
  49. return nf.format(value).replaceAll("\\.","");
  50. }
  51. static public String formatValutaNoSeparator(float value)
  52. {
  53. DecimalFormatSymbols symbols = new DecimalFormatSymbols();
  54. symbols.setDecimalSeparator(',');
  55. symbols.setGroupingSeparator('.');
  56. DecimalFormat nf = new DecimalFormat("#,##0.00", symbols);
  57. return nf.format(value).replaceAll("\\.","");
  58. }
  59. static public String formatValutaNoSeparator(int value)
  60. {
  61. DecimalFormat nf = new DecimalFormat("###");
  62. return nf.format(value).replaceAll("\\.","");
  63. }
  64. static public String formatDecimalValue(int value)
  65. {
  66. DecimalFormatSymbols symbols = new DecimalFormatSymbols();
  67. symbols.setGroupingSeparator('.');
  68. symbols.setDecimalSeparator(',');
  69. DecimalFormat nf = new DecimalFormat("#,##0", symbols);
  70. return nf.format(value);
  71. }
  72. static public String formatValue(int value)
  73. {
  74. DecimalFormat nf = new DecimalFormat("###");
  75. return nf.format(value);
  76. }
  77. static public String formatValue(double value)
  78. {
  79. DecimalFormatSymbols symbols = new DecimalFormatSymbols();
  80. symbols.setDecimalSeparator(',');
  81. DecimalFormat nf = new DecimalFormat("##0.00");
  82. return nf.format(value);
  83. }
  84. static public String formatDate(Date date)
  85. {
  86. return new SimpleDateFormat("dd/MM/yyyy").format(date);
  87. }
  88. static public String formatDate(Date date, String pattern)
  89. {
  90. if(pattern == null)
  91. {
  92. pattern = "dd/MM/yyyy";
  93. }
  94. return new SimpleDateFormat(pattern).format(date);
  95. }
  96. static public String formatDateTime(Date date)
  97. {
  98. return new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
  99. }
  100. static public String formatTime(Date date)
  101. {
  102. return new SimpleDateFormat("HH:mm:ss").format(date);
  103. }
  104. static public String formatTime(Date date, String separator)
  105. {
  106. String pattern = "HH:mm:ss";
  107. if(separator != null)
  108. {
  109. pattern = pattern.replaceAll(":", separator);
  110. }
  111. return new SimpleDateFormat(pattern).format(date);
  112. }
  113. static public String formatBoolean(boolean b)
  114. {
  115. return Boolean.toString(b);
  116. }
  117. //
  118. // static public String formatDate(String date) {
  119. // try {
  120. // return formatDate(new SimpleDateFormat("dd/MM/yyyy").parse(date));
  121. // } catch (ParseException e) {
  122. // try {
  123. // return formatDate(new SimpleDateFormat().parse(date));
  124. // } catch (ParseException e1) {
  125. // return formatText(date);
  126. // }
  127. // }
  128. // }
  129. public static String formatNumber2Dec(String sValue)
  130. {
  131. String model = "###,###,###,###,##0.00";
  132. return formatNumberModel(sValue, model);
  133. }
  134. public static String formatNumberModel(String sValue, String model)
  135. {
  136. String formatVal = "";
  137. if (sValue == null || sValue.equals(""))
  138. return "";
  139. DecimalFormatSymbols mySymbol = new DecimalFormatSymbols();
  140. mySymbol.setDecimalSeparator(',');
  141. mySymbol.setGroupingSeparator('.');
  142. DecimalFormat myFormatter = new DecimalFormat(model, mySymbol);
  143. myFormatter.setGroupingSize(3);
  144. formatVal = myFormatter.format(new Double(sValue));
  145. return formatVal;
  146. }
  147. public static String unformatNumber(String sValue)
  148. {
  149. try
  150. {
  151. String formatVal = "";
  152. if (sValue == null || sValue.equals(""))
  153. return "";
  154. //sValue = StringUtility.ReplaceSubString(sValue,".","");
  155. //sValue = StringUtility.ReplaceSubString(sValue,",",".");
  156. sValue = sValue.replaceAll("\\.", "").replaceAll(",", ".");
  157. DecimalFormatSymbols mySymbol = new DecimalFormatSymbols();
  158. mySymbol.setDecimalSeparator('.');
  159. String model = "################.################";
  160. DecimalFormat myFormatter = new DecimalFormat(model, mySymbol);
  161. myFormatter.setGroupingSize(16);
  162. formatVal = myFormatter.format(new Double(sValue));
  163. return formatVal;
  164. }
  165. catch (Exception e)
  166. {
  167. e.printStackTrace();
  168. return "";
  169. }
  170. }
  171. public static String formatPercentuale(double value) throws TransformException
  172. {
  173. DecimalFormatSymbols dfs = new DecimalFormatSymbols();
  174. dfs.setDecimalSeparator(',');
  175. NumberFormat format = new DecimalFormat("###.0###",dfs);
  176. //imposto il numero di decimali per la formattazione
  177. if(NullValue.NUMBER == 3)
  178. {
  179. format.setMaximumFractionDigits(3);
  180. format.setMinimumFractionDigits(3);
  181. }
  182. else
  183. {
  184. format.setMaximumFractionDigits(3);
  185. format.setMinimumFractionDigits(3);
  186. }
  187. format.setMaximumIntegerDigits(3);
  188. format.setMinimumIntegerDigits(1);
  189. format.setGroupingUsed(false);
  190. String fValue = format.format(value);
  191. // Se il numero di decimali ? automatico...
  192. if(NullValue.NUMBER == 3)
  193. {
  194. // ...elimina gli zero meno significativi
  195. while(fValue.endsWith("0"))
  196. fValue = fValue.substring(0,fValue.length()-1);
  197. if(fValue.endsWith(","))
  198. fValue = fValue.substring(0,fValue.length()-1);
  199. }
  200. return fValue;
  201. }
  202. public static String formatAliquota(double value) throws TransformException
  203. {
  204. DecimalFormatSymbols dfs = new DecimalFormatSymbols();
  205. dfs.setDecimalSeparator(',');
  206. NumberFormat format = new DecimalFormat("###.0####",dfs);
  207. //imposto il numero di decimali per la formattazione
  208. format.setMaximumFractionDigits(4);
  209. format.setMinimumFractionDigits(4);
  210. format.setMaximumIntegerDigits(3);
  211. format.setMinimumIntegerDigits(1);
  212. format.setGroupingUsed(false);
  213. String fValue = format.format(value);
  214. // Se il numero di decimali ? automatico...
  215. if(NullValue.NUMBER == 4)
  216. {
  217. // ...elimina gli zero meno significativi
  218. while(fValue.endsWith("0"))
  219. fValue = fValue.substring(0,fValue.length()-1);
  220. if(fValue.endsWith(","))
  221. fValue = fValue.substring(0,fValue.length()-1);
  222. }
  223. return fValue;
  224. }
  225. public static String formatSuperDouble(double value) throws TransformException
  226. {
  227. DecimalFormatSymbols dfs = new DecimalFormatSymbols();
  228. dfs.setDecimalSeparator(',');
  229. NumberFormat format = new DecimalFormat("###.0##########",dfs);
  230. //imposto il numero di decimali per la formattazione
  231. format.setMaximumFractionDigits(10);
  232. format.setMinimumFractionDigits(10);
  233. format.setMaximumIntegerDigits(1);
  234. format.setMinimumIntegerDigits(1);
  235. format.setGroupingUsed(false);
  236. String fValue = format.format(value);
  237. // // Se il numero di decimali ? automatico...
  238. // if(NullValue.NUMBER == 4)
  239. // {
  240. // // ...elimina gli zero meno significativi
  241. // while(fValue.endsWith("0"))
  242. // fValue = fValue.substring(0,fValue.length()-1);
  243. // if(fValue.endsWith(","))
  244. // fValue = fValue.substring(0,fValue.length()-1);
  245. // }
  246. return fValue;
  247. }
  248. /** Versione 'semplice' di una sprintf
  249. * @param fmt la stringa di formattazione
  250. * @param params i parametri
  251. * @return la stringa formattata
  252. */
  253. final static public String simpleSprintf(final String fmt, final Object ... params)
  254. {
  255. return String.format(fmt.replaceAll("\\{[0-9]*\\}", "\\%s"), params);
  256. }
  257. /**
  258. * Restituisce un array di stringe divise secondo la lunghezza <len> massima
  259. * @param str
  260. * @param len
  261. * @return
  262. */
  263. public static List<String> formatTextStrutturaPDF(String str, int len)
  264. {
  265. if(StringUtils.isEmpty(str) || len == 0)
  266. return new ArrayList<String>();
  267. final String SEPARATOR = ", ";
  268. String[] aStr = str.split(SEPARATOR);
  269. List<String> result = new ArrayList<String>();
  270. for(int index = 0, size = aStr.length; index < size; index++)
  271. {
  272. if(result.size() == 0)
  273. result.add(aStr[index] + SEPARATOR);
  274. else if(!StringUtils.isEmpty(aStr[index]))
  275. {
  276. String tmp = result.get(result.size()-1);
  277. if((tmp.length() + aStr[index].length()) <= len)
  278. {
  279. result.remove(result.size()-1);
  280. tmp = tmp.substring(0, tmp.length()-2);
  281. result.add(tmp + SEPARATOR + aStr[index] + SEPARATOR);
  282. }
  283. else
  284. {
  285. result.add(aStr[index] + SEPARATOR);
  286. }
  287. }
  288. }
  289. if(result.size() > 0){
  290. String tmp = result.get(result.size()-1);
  291. tmp = tmp.substring(0, tmp.length()-2);
  292. result.remove(result.size()-1);
  293. result.add(tmp);
  294. }
  295. return result;
  296. }
  297. /**
  298. * Restituisce la Stringa passata come parametro troncata dopo #len caratteri
  299. * @param str
  300. * @param len
  301. * @return
  302. */
  303. public static String trunkString(String str, int len)
  304. {
  305. if(StringUtils.isEmpty(str))
  306. return NullValue.STRING;
  307. str = str.trim();
  308. if(str.length() <= len)
  309. return formatText(str);
  310. return formatText(str.substring(0, len))+"...";
  311. }
  312. public static String formatNumeroTessera(int numeroTessera) {
  313. return String.format("%07d", numeroTessera);
  314. }
  315. }