-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatoUtil.java
More file actions
269 lines (239 loc) · 6.96 KB
/
FormatoUtil.java
File metadata and controls
269 lines (239 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Pattern;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
/**
* Classe utilitária de formatos.
*/
public class FormatoUtil {
private FormatoUtil() {
}
/**
* @param texto
* @return
* @throws FormatException
*/
public static Date strindDataSemBarraFormataDate(final String texto) throws Exception {
return DateUtils.getData(texto.replaceAll("(\\d{2})(\\d{2})(\\d{4})", "$1/$2/$3"), DateUtils.FORMATO_DATA, false);
}
/**
* @param texto
* @return
*/
public static String strindFormatarHora(final String texto) {
return texto.replaceAll("(\\d{2})(\\d{2})(\\d{2})", "$1:$2:$3");
}
/**
* @param valor
* @return
*/
public static String numeroDecimalFormatado(final Integer valor) {
return numeroDecimalFormatado(valor, "00");
}
/**
* @param valor
* @param formato
* @return
*/
public static String numeroDecimalFormatado(final Integer valor, final String formato) {
DecimalFormat format = new DecimalFormat(formato);
return format.format(valor);
}
/**
* @param mascara
* @param texto
* @return
*/
public static String formataString(final String mascara, final String texto) {
String retorno = "";
MaskFormatter format;
try {
format = new MaskFormatter(mascara);
format.setValidCharacters("0123456789");
JFormattedTextField text = new JFormattedTextField();
text.setText(texto);
retorno = text.getText();
} catch (ParseException e) {
e.printStackTrace();
}
return retorno;
}
/**
* @param valor
* @return
*/
public static boolean verificacaoStringNumerica(final String valor) {
return Pattern.matches("[-]?\\d*[.]?\\d+", valor.replace(",", "."));
}
/**
* Concatenar Strings com o separador passado, pulando as strings vazias
*
* @param separador
* Separador a ser adicionado entre os valores passados
* @param values
* Valores a serem concatenados
* @return String concatenada
*/
public static String concat(String separador, String... values) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < values.length; i++) {
if (values[i] != null && !values[i].equalsIgnoreCase("")) {
str.append(values[i]);
if (values.length > (i + 1)) {
// Verifica se o próximo é o ultimo, e se está em branco
boolean proximoEhUltimo = values.length == i + 2;
boolean ultimoEhBranco = values[i + 1].equalsIgnoreCase("");
if (!(proximoEhUltimo && ultimoEhBranco)) {
str.append(separador);
}
}
}
}
return str.toString();
}
/**
* Concatenar Strings com ponto "."
*
* @param values
* Valores a serem concatenados
* @return String concatenada
*/
public static String concatPoint(String... values) {
return concat(".", values);
}
/**
* Converter o InputStream para Array de Byte
*
* @param input
* InputStream
* @return byte[] Array de Byte
* @throws IOException
*/
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int len = 0;
while ((len = input.read(buf)) > -1) {
output.write(buf, 0, len);
}
return output.toByteArray();
}
/**
* Converter Double para String no formato #.##0,00
*
* @param valor
* a ser formatado
* @return String formatada #.##0,00
*/
public static String formatDoubleDecimal(final Double valor) {
DecimalFormat df = new DecimalFormat("#,##0.00");
return df.format(valor);
}
/**
* Formata o caminho do hibernate para o mesmo do alias do Criteria
*
* @param propertyName
* @return
*/
public static String formatPropertyAlias(final String propertyName) {
if (!propertyName.contains(".")) {
return propertyName;
}
String campo = propertyName.substring(propertyName.lastIndexOf('.'));
String caminho = propertyName.substring(0, propertyName.lastIndexOf(campo)).replace(".", "_");
return caminho.concat(campo);
}
/**
* Formata o caminho do hibernate para o mesmo do alias do Criteria
*
* @param propertyName
* @return
*/
public static String getEntity(final String propertyName) {
return propertyName.substring(0, propertyName.lastIndexOf("."));
}
/**
* Formata o número Double para uma String do valor com duas casas decimais
*
* @param valor
* @return
*/
public static String numeroComDuasCasasDecimais(Double valor) {
return numeroCasasDecimais(valor, 2);
}
/**
* Formata o número Double para uma String do valor com a quantidade de casas decimais informada
*
* @param valor
* @param casasDecimais
* @return
*/
public static String numeroCasasDecimais(Double valor, int casasDecimais) {
DecimalFormatSymbols ds = new DecimalFormatSymbols();
ds.setDecimalSeparator(',');
ds.setGroupingSeparator('.');
DecimalFormat format = new DecimalFormat();
format.setDecimalFormatSymbols(ds);
format.setMaximumFractionDigits(casasDecimais);
format.setMinimumFractionDigits(1);
return format.format(valor);
}
/**
* Método retorna o valor decimal de um número, ignorando o número inteiro
*
* @param valor
* @return
*/
public static double getValorDecimal(Double valor) {
return valor % 1;
}
/**
* Verificar se o valor passado é um número inteiro ou fracionado
*
* @param valor
* @return
*/
public static boolean isNumeroInteiro(Double valor) {
return getValorDecimal(valor) == 0;
}
/**
* Converter lista de sortBy passada para um Array <br>
* OBS: Esta lista deve ser passada no campo SortBy de uma DataTable ou de Uma coluna <br>
* <b>EXEMPLO: sortBy="#{beanVar.campo1};#{beanVar.campo2};#{beanVar.campo3}" </b><br>
* @param sortField
* @return
*/
public static String[] getArraySortFields(String sortField) {
String[] fields = sortField.split(";");
String[] retorno = new String[fields.length];
retorno[0] = fields[0].replace("}", "");
if (fields.length > 1) {
for (int i = 1; i < fields.length; i++) {
String str = fields[i];
if (str.indexOf(".") > -1) {
str = str.replace("}", "").replace("#{", "");
str = str.substring(str.indexOf(".") + 1);
}
retorno[i] = str;
}
}
return retorno;
}
/**
* Resgatar um número double formatado sem separação e com casas decimais
* @param value
* @return
*/
public static Double getDoubleCasasDecimais(Double value, int casasDecimais) {
DecimalFormat df = new DecimalFormat("###0.00");
df.setMaximumFractionDigits(casasDecimais);
return new Double(df.format(value).replace(",", "."));
}
}