Convert a to_date sql to a date java

0

My problem is this, I have to grab SQL insert lines and check if the formats in the TO_DATE function in these inserts are valid with a java method. For a 'DD / MM / YYYY' format, SimpleDateFormat solves my problem, but sometimes an insert is required in 'DD / MM / YYYY format HH24: MI: SS' and there SimpleDateFormat does not accept this format, how can I see this ? Here is the code for my Java method.

boolean retorno = true;
    try {
        if (line.matches(".*TO_DATE.*")) {
            String[] linha_quebrada = line.split("TO_DATE");
            ArrayList<String> datas = new ArrayList<String>();
            for (int i = 0; i < linha_quebrada.length; i++) {
                if (linha_quebrada[i].substring(0, 1).equalsIgnoreCase("(")) {
                    datas.add(linha_quebrada[i]);
                }
            }
            for (int k = 0; k < datas.size(); k++) {
                String toDate = null;
                int inicio;
                int fim;
                if (datas.get(k).substring(0, 1).equalsIgnoreCase("(")) {
                    toDate = datas.get(k).substring(0, datas.get(k).length() - 1);
                    inicio = toDate.indexOf("(") + 1;
                    fim = toDate.indexOf(")");
                    toDate = toDate.substring(inicio, fim);
                    String[] data_formato = toDate.split(",");
                    String data = data_formato[0].replace("'", "");
                    String formato = data_formato[1].replace("'", "").trim();
                    /*java.sql.Date dateSQL = new Date*/
                    SimpleDateFormat formatoData = new SimpleDateFormat(formato);
                    Date dataJava = formatoData.parse(data);
                    System.out.println(dataJava.toString());
                }
            }
        }
    } catch (ParseException e) {
        retorno = false;
    }
    
asked by anonymous 07.07.2017 / 02:12

1 answer

1

The problem is that this format 'DD / MM / YYYY HH24: MI: SS' is not compatible with SimpleDateFormat, it has to be one. For example:

'dd / MM / yyyy HH: mm: ss' > 05/05/2014 23:10:55

'dd / MM / yyyy hh: mm: ss' > 05/05/2014 11:10:55

One of these will solve your problem.

    
07.07.2017 / 04:12