JTextField field [closed]

-1

How to convert a JTextField component to type Date ? How to do research? How to create a query for the database by returning a list of requests according to a date range?

Follow the code currently used:

if (opcaoBusca.getSelectedIndex() == 0) {
   JOptionPane.showMessageDialog(null,
        "Escolha uma Opção de Busca!");
} else if (opcaoBusca.getSelectedIndex() == 1) {
   RS = stmt.executeQuery("select numped FROM PCPEDC WHERE DATA > '10/01/2014' and numped =  " + BuscaCodigo);
   while (RS.next()) {
      int Num = RS.getInt("numped");

      consulta = false;
      JOptionPane.showMessageDialog(null, "Dados Encontrado!!!!");
   }
} else if (opcaoBusca.getSelectedIndex() == 2) {
   RS = stmt.executeQuery("SELECT Data FROM PCPEDC WHERE Data BETWEEN " + Dtincial + "AND" + Dtfinal);

   while (RS.next()) {
      int Num = RS.getInt("numped");
      //...
   }
}


//mascara para os campos //
Dtincial = new JFormattedTextField(DateFormat.getDateInstance( ));
try {  
   javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("##/##/####");
   Dtincial = new javax.swing.JFormattedTextField(data);        
} catch (Exception e){
}

Dtincial.setColumns(10);
Dtfinal = new JFormattedTextField(DateFormat.getDateInstance( ));
try {  
   javax.swing.text.MaskFormatter data= new javax.swing.text.MaskFormatter("##/##/####");
   Dtfinal  = new javax.swing.JFormattedTextField(data);        
} catch (Exception e){
}

//declaração dos atributos
private JTextField Dtincial;
private JTextField Dtfinal;
    
asked by anonymous 14.01.2015 / 17:51

1 answer

2

SimpleDateFormat

Documentation and Patterns: SimpleDateFormat .

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

// Valor no textfield. Entrada: 14/01/2015
String text= textField.getText();

Date date = dateFormat.parse(text);

System.out.println(date.toString); //Wed Jan 14 00:00:00 BRST 2015
System.out.println(date.toInstant().toString()); // 2015-01-14T02:00:00Z

DateFormat

// Entrada: 01/01/2015
String text= textField.getText();
Date date = null;

try {
   date = DateFormat.getDateInstance().parse(text);
} catch (ParseException e){
   // Tratamento de exceção.
}
System.out.println(date.toString()); // Thu Jan 01 00:00:00 BRST 2015
System.out.println(date.toInstant().toString()); // 2015-01-01T02:00:00Z

In this case the conversion is done for Date of package java.util , if I'm not mistaken Statements and PreparedStaments receive an object Date of package% with%. If so, see this question not StackOverflow-en.

    
14.01.2015 / 21:26