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.util.VerificadorUtil; |
||
| 8 | |||
| 9 | public abstract class SrvConversorDeTipo extends br.gov.al.saude.framework.core.util.ConversorDeTipo { |
||
| 10 | |||
| 11 | public static final String FORMATO_DATA_DD_MM_YYYY = "dd/MM/yyyy"; |
||
| 12 | public static final String FORMATO_DATE_DD_MM_YYYY_HH_MM = "dd/MM/yyyy HH:mm"; |
||
| 13 | public static final String SIM = "SIM"; |
||
| 14 | public static final String NAO = "NÃO"; |
||
| 15 | |||
| 16 | public static Long converterStringParaLong(String valor) { |
||
| 17 | return VerificadorUtil.naoEstaNuloOuVazio(valor) ? Long.parseLong(valor) : null; |
||
| 18 | } |
||
| 19 | |||
| 20 | public static Boolean converterStringParaBoolean(String valor) { |
||
| 21 | return (VerificadorUtil.estaNulo(valor)) ? null : (VerificadorUtil.naoEstaNuloOuVazio(valor) && valor.equals(SIM))? true : false; |
||
| 22 | } |
||
| 23 | |||
| 24 | public static String converterBooleanParaString(Boolean valor) { |
||
| 25 | return valor ? SIM : NAO; |
||
| 26 | } |
||
| 27 | |||
| 28 | public static Integer converterStringParaInteger(String valor) { |
||
| 29 | return VerificadorUtil.naoEstaNuloOuVazio(valor) ? new Integer(valor) : null; |
||
| 30 | } |
||
| 31 | |||
| 32 | public static Date converterStringParaDate(String valor) { |
||
| 33 | return VerificadorUtil.naoEstaNuloOuVazio(valor) ? parseDate(valor, FORMATO_DATA_DD_MM_YYYY) : null; |
||
| 34 | } |
||
| 35 | |||
| 36 | public static Date converterStringParaDateTime(String valor) { |
||
| 37 | return VerificadorUtil.naoEstaNuloOuVazio(valor) ? parseDate(valor, FORMATO_DATE_DD_MM_YYYY_HH_MM) : null; |
||
| 38 | } |
||
| 39 | |||
| 40 | public static String converterDateParaString(Date data, String pattern) { |
||
| 41 | if(VerificadorUtil.naoEstaNuloOuVazio(data)) { |
||
| 42 | SimpleDateFormat format = new SimpleDateFormat(pattern); |
||
| 43 | return format.format(data); |
||
| 44 | } |
||
| 45 | |||
| 46 | return null; |
||
| 47 | } |
||
| 48 | |||
| 49 | private static Date parseDate(String dataString, String pattern) { |
||
| 50 | SimpleDateFormat format = new SimpleDateFormat(pattern); |
||
| 51 | |||
| 52 | try { |
||
| 53 | return format.parse(dataString); |
||
| 54 | } catch (ParseException pe) { |
||
| 55 | throw new RuntimeException("Não foi possível converter String para Date"); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |