Campo Jtextfield

Asked

Viewed 290 times

-1

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

Follows 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;
  • Sorry guy but I didn’t understand this process, I did similar thing to this and it didn’t work

  • I’ll put a code up here

  • more in the case when setting the date it is giving error, like this: ps.setDate(1, Dtincial); ps.setDate(2, Dtfinal);

  • someone to help?

  • 1

    You’re looking to pass one JFormattedTextField for a method that receives a Date, will give even error. You have to catch the value of the textfield and not the object.

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 that case the conversion is made to the Date package java.util, if I’m not mistaken Statements and Preparedstaments receive an object Date package java.sql. If that’s what it is, look that question in Stackoverflow-en.

Browser other questions tagged

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