Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | package br.gov.al.saude.test.util; |
| 2 | |||
| 3 | import java.text.ParseException; |
||
| 4 | import java.text.SimpleDateFormat; |
||
| 5 | import java.util.Date; |
||
| 6 | |||
| 7 | import br.gov.al.saude.framework.core.exception.NegocioException; |
||
| 8 | import br.gov.al.saude.framework.core.util.VerificadorUtil; |
||
| 9 | |||
| 10 | public class ConversorDeTipo extends br.gov.al.saude.framework.core.util.ConversorDeTipo { |
||
| 11 | |||
| 12 | public static final String FORMAT_DATE_DD_MM_YYYY = "dd/MM/yyyy"; |
||
| 13 | public static final String FORMAT_DATE_DD_MM_YYYY_HH_MM_SS = "dd/MM/yyyy HH:mm:ss"; |
||
| 14 | public static final String FORMAT_DATE_DD_MM_YYYY_HH_MM = "dd/MM/yyyy HH:mm"; |
||
| 15 | private static final String STRING_VAZIA = ""; |
||
| 16 | |||
| 17 | public static Long converterStringParaLong(String valor) { |
||
| 18 | return valor!=null? Long.parseLong(valor) : null; |
||
| 19 | } |
||
| 20 | |||
| 21 | public static String converterDateParaString(Date data, String pattern) { |
||
| 22 | if(VerificadorUtil.naoEstaNuloOuVazio(data)) { |
||
| 23 | SimpleDateFormat format = new SimpleDateFormat(pattern); |
||
| 24 | return format.format(data); |
||
| 25 | } |
||
| 26 | return null; |
||
| 27 | } |
||
| 28 | |||
| 29 | public static Date converterStringParaDate(String data, String pattern) { |
||
| 30 | return VerificadorUtil.naoEstaNuloOuVazio(data)? parseDate(data, pattern) : null; |
||
| 31 | } |
||
| 32 | |||
| 33 | private static Date parseDate(String data, String pattern) { |
||
| 34 | try { |
||
| 35 | return new SimpleDateFormat(pattern).parse(data); |
||
| 36 | } catch (ParseException e) { |
||
| 37 | throw new NegocioException("Não foi possível converter a Data."); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | public static Integer converterStringParaInteger(String valor) { |
||
| 42 | return valor!=null? Integer.parseInt(valor) : null; |
||
| 43 | } |
||
| 44 | |||
| 45 | public static String converterIntegerParaString(Integer valor) { |
||
| 46 | return valor!=null? valor.toString() : null; |
||
| 47 | } |
||
| 48 | |||
| 49 | public static Boolean converterStringParaBoolean(String valor) { |
||
| 50 | if(VerificadorUtil.naoEstaNuloOuVazio(valor)) { |
||
| 51 | if("true".equalsIgnoreCase(valor) || "false".equalsIgnoreCase(valor)) { |
||
| 52 | return Boolean.parseBoolean(valor); |
||
| 53 | } |
||
| 54 | if("SIM".equalsIgnoreCase(valor)) { |
||
| 55 | return true; |
||
| 56 | } |
||
| 57 | if("NAO".equalsIgnoreCase(valor) || "NÃO".equalsIgnoreCase(valor)) { |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | return null; |
||
| 62 | } |
||
| 63 | |||
| 64 | public static String converterBooleanParaString(Boolean valor) { |
||
| 65 | if(VerificadorUtil.naoEstaNulo(valor)) { |
||
| 66 | if(valor) { |
||
| 67 | return "Sim"; |
||
| 68 | } |
||
| 69 | return "Não"; |
||
| 70 | } |
||
| 71 | return null; |
||
| 72 | } |
||
| 73 | |||
| 74 | public static <T> String converterParaStringVaziaQuandoNulo(T valor) { |
||
| 75 | return VerificadorUtil.naoEstaNulo(valor)? valor.toString() : STRING_VAZIA; |
||
| 76 | } |
||
| 77 | } |