Create method that takes the time entered

0

How can I create a method that takes the time typed in a field?

It will grab the contents of the field to save to the database. I use pojo and Dao.

I made a well-summed example, what I need to do is a method that takes this String content and can save as Time or TimeStamp in the database.

What I tried to do was:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;

public class Hora extends JFrame {

    public static void main(String[] args) {
        Hora t = new Hora();
        t.setVisible(true);
    }

    private Pojo pojo = new Pojo();

    JFormattedTextField campoHora = new JFormattedTextField();
    JButton pegar = new JButton("Pegar");

    public Hora() {
        setSize(450, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        try {
            MaskFormatter mf = new MaskFormatter("##:##:##");
            mf.install(campoHora);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JPanel painel = new JPanel();
        painel.add(campoHora);
        campoHora.setPreferredSize(new Dimension(80, 22));

        painel.add(pegar);
        pegar.setPreferredSize(new Dimension(75, 22));
        acao();

        add(painel);
    }

    private void pegaPojo() {
        pojo.setHora(campoHora.getValorTime());
    }

    private void acao() {
        pegar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                //campoHora.getValorTime();
            }
        });
    }

    private Time getValorTime() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            return sdf.parse(getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Não foi possível obter a hora!");
            e.printStackTrace();
            return null;
        }
    }
}

class Pojo {

    private Time hora;

    public Time getHora() {
        return hora;
    }

    public void setHora(Time hora) {
        this.hora = hora;
    }
}
    
asked by anonymous 01.07.2017 / 00:47

1 answer

0

I made few changes to the code and it worked without problems, taking the field time in the click:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.text.MaskFormatter;

public class Hora extends JFrame {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            Hora t = new Hora();
            t.setVisible(true);
        });
    }

    JFormattedTextField campoHora = new JFormattedTextField();
    JButton pegar = new JButton("Pegar");

    public Hora() {
        setSize(450, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        try {
            MaskFormatter mf = new MaskFormatter("##:##:##");
            mf.install(campoHora);

        } catch (Exception e) {
            e.printStackTrace();
        }
        JPanel painel = new JPanel();
        painel.add(campoHora);
        campoHora.setPreferredSize(new Dimension(80, 22));

        painel.add(pegar);
        pegar.setPreferredSize(new Dimension(75, 22));
        acao();

        add(painel);
    }

    private void acao() {
        pegar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                    System.out.println(new java.sql.Timestamp(sdf.parse(campoHora.getText()).getTime()));
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Não foi possível obter a hora!");
                    campoHora.setText("");
                    e.printStackTrace();
                }
            }
        });
    }
}

In this way, it transforms type Date to Timestamp , which can be saved in a similar field on your bank.

But I would do it using JSpinners , so there you can better control what is informed, without having to deal with whether the time is valid or not. If you are interested, here is a use example of it only with hours, minutes, and seconds.

    
01.07.2017 / 01:46