Convert a sql to_date to a java date

Asked

Viewed 90 times

0

My problem is the following, I have to take rows of SQL Insert and check whether 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 it is necessary an 'DD/MM/YYYY HH24:MI:SS' format and then Simpledateformat does not accept this format, how can I see it? Follow the code of 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;
    }

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' > 01/05/2014 23:10:55

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

One of these will solve your problem.

  • I missed this link in the answer: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

  • Resolve, but wanted to know a way to check all types of masks that exist in SQL, will have a way to put all of them so I can validate in this method that I created?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.