从MySQL数据库获取Date对象,并将其显示在格式化的文本字段中

时间:2019-02-07 07:32:31

标签: java mysql hibernate

我正在尝试从数据库中获取出生日期并将其显示在JFormattedTextField上。它应以mm / dd / yyyy格式显示,但以yyyy / dd / mm格式存储。我将如何处理?患者的出生日期存储为java.sql.Date

    JLabel lbl3 = new JLabel("Date of Birth:");
    lbl3.setBounds(17, 129, 90, 22);
    lbl3.setForeground(Color.WHITE);
    lbl3.setFont(new Font("Microsoft New Tai Lue", Font.PLAIN, 16));

    JFormattedTextField formattedDob = new JFormattedTextField();
    formattedDob.setEditable(false);
    formattedDob.setText("//");
    formattedDob.setBounds(201, 128, 183, 22);
    contentPane.add(formattedDob);  

JButton btnSearchPatient = new JButton("Search");
        btnSearchPatient.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                PatientSQL searchPatient = new PatientSQL();
                Patient staff = searchPatient.getBySSN(patient.getSsn());

            txtAddress1.setText(staff.getAddress1());
            txtAddress2.setText(staff.getAddress2());
            txtCity.setText(staff.getCity());
        }
    });

这是我的PatientSQL.java供参考(不相关的代码,例如软件包和函数已删除,以便于阅读):

public class PatientSQL {
    private SessionFactory factory;

    public PatientSQL() {
        try {
            factory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);

            throw new ExceptionInInitializerError(ex);
        }
    }

    /* Method to CREATE a patient in the database */
    public void add(String fname, String lname, String ssn, java.sql.Date dob, String phoneNo, String address1, String address2, String city, String zipcode, String allergy1, String allergy2, String allergy3, String photo) {
        Patient patient = new Patient(fname, lname, ssn, dob, phoneNo, address1, address2, city, zipcode, allergy1, allergy2, allergy3, photo);

        add(patient);
    }

    public Patient getBySSN(String ssn) {
        Session session = factory.openSession();
        Patient patient = null;

        try {
            patient = (Patient) session.get(Patient.class, ssn);

        } catch (HibernateException e) {
            System.err.println("Can not find patient ssn: " + ssn);
        } finally {
            session.close();
        }

        return patient;
    }

}

3 个答案:

答案 0 :(得分:0)

也许您应该尝试转换日期: 使用LocalDateTime#parse()(如果字符串恰巧包含时区部分,则使用ZonedDateTime#parse())将特定模式的String解析为LocalDateTime。 喜欢:

String date_s = "2011/01/18"; 
SimpleDateFormat dt = new SimpleDateFormat("yyyy/dd/mm"); 
Date date = dt.parse(date_s); 
SimpleDateFormat dt1 = new SimpleDateFormat("mm/dd/yyyy");
System.out.println(dt1.format(date));

答案 1 :(得分:0)

愚蠢的要求要求愚蠢的答案:

CREATE TABLE patient (
...
dob_string VARCHAR(8),
dob_date DATE AS (STR_TO_DATE(dob_string,"%Y/%d/%m"))
)

因此,将其存储到dob_string中,然后从dob_date获取,然后根据需要输出格式。

答案 2 :(得分:0)

如果可以使用SimpleDateFormat

SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
df.format(patient.getDob()); //I'm guessing what the get method is named but you get what I mean