简体   繁体   English

Java:将日期从JFormattedTextField掩码保存到Access DB

[英]Java : Saving Date from JFormattedTextField MAsk to Access DB

I have a custom JFormattedTextfield for date that uses MaskFormatter __/__/____ and patttern of "dd/MM/yyyy". 我有一个使用MaskFormatter __/__/____和“ dd / MM / yyyy”样式的日期的自定义JFormattedTextfield。
To save to an object I use : 要保存到对象,我使用:

        try {
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        String dtStr = this.dateFormatTf.getValue().toString().trim();  // 01/01/2011
        java.util.Date dt =  df.parse(dtStr);  // dt = Sun Jan 01 00:00:00 IST 2011

        bk.setTransDate(dt);  // SAVes the date as util.Date in above way as dt
    } catch(java.text.ParseException e) { 
        Utility.logs.log(Level.WARNING, "Parse Date Exception = " + e.getMessage());
        e.printStackTrace();
        return null;
    }

After saving to an object, I try to generate sql insert command. 保存到对象后,我尝试生成sql insert命令。 Here, if I pass util.Date object, it gives exception as it can't store "Sun Jan 01 00:00:00 IST 2011" in DATE/TIME field of MS Access. 在这里,如果我传递util.Date对象,它将给出异常,因为它无法在MS Access的DATE / TIME字段中存储“ Sun Jan 01 00:00:00 IST 2011”。 Hence I tried the below to get sql.Date object : 因此,我尝试了以下方法来获取sql.Date对象:

     long tm = bk.getTransDate().getTime();
    java.sql.Date dt = new java.sql.Date(tm);

and pass dt to insert sql command. 并通过dt插入sql命令。 This saves the date in MS Access but the value stored is "01-07-1905". 这样可以将日期保存在MS Access中,但是存储的值为“ 01-07-1905”。

What changes are required and where to make the date store in proper format from textfield to MS Access DB. 从文本字段到MS Access DB,需要进行哪些更改以及以正确的格式存储日期的位置。 In Access DB, the DATE field's format is set to SHORT. 在Access DB中,DATE字段的格式设置为SHORT。

Any help is highly appreciated. 非常感谢您的帮助。

Thanks to all, 谢谢大家,

I have seen Date issue in MS Access many times. 我已经多次在MS Access中看到日期问题。 Mostly I convert it to String/Text data type to help me store properly & cast as Date object properly in Application. 通常,我将其转换为String / Text数据类型,以帮助我正确存储并在Application中正确转换为Date对象。

Well, this time also I ended using the same solution. 好吧,这一次我也结束了使用相同的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM