【问题标题】:Eliminate time value from date field and change the format while fetching data from database从日期字段中删除时间值并在从数据库中获取数据时更改格式
【发布时间】:2017-05-13 13:56:03
【问题描述】:

我的程序从 Access 数据库中获取几列,并在 JTable 中显示数据。它工作正常,但我需要在数据显示时进行一些更改。就像,尽管在 Access 数据库中以 dd-MM-yyyy 的形式存储数据,但它在检索时以 yyyy-MM-dd tt:tt:tt 格式显示。我需要在表格中以dd-MM-yyyy的形式显示。此日期字段在表的第一列中表示为for_date

sql = "Select for_date as FOR_DATE,outage_time as OUTAGE_TIME,stat_detail as STATION_DETAILS from " + table_sel + " where for_date='" + date1 + "' and stat_detail ='" + combo_sel + "'";
Connection con = null;
Statement st = null;
ResultSet rs = null;
PreparedStatement pst = null;
String dbURL = "jdbc:ucanaccess://C:\\Users\\Dell_PC\\Documents\\SYSTEM_OUTAGE_REPORT.accdb";
con = DriverManager.getConnection(dbURL);
st = con.createStatement();
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
con.close(); 

【问题讨论】:

    标签: java mysql swing date ms-access


    【解决方案1】:

    我在这里有点含糊,因为我没有使用 UCanAccess JDBC 驱动程序的经验。据我了解,您正在使用它。但是,我相信只要没有人提到解决此任务的现代方法,这个答案就不完整。现代方法包括尽量减少对过时类 Date 的使用,如果可以的话完全放弃它,并改用 java.time.LocalDate

    所以第一步是检查新版本的UCanAccess是否符合JDBC 4.2。如果是这样,它可以直接给你一个LocalDate 对象而不是Date 对象。所以我读过。如果这不是一个选项,那么在您获得Date 之后的第一件事就是将其转换为yourDate.toLocalDate()。这应该是你DbUtils.resultSetToTableModel() 的工作。但是我不确定这是您自己修改的方法还是某些标准库中的方法。

    有了LocalDate,我们就快到了。这将默认显示为yyyy-MM-dd。你想要反过来,dd-MM-yyyy。为此,我倾向于同意 Robin 和 camickr 的观点,即您应该使用表格渲染器。你的渲染器应该使用一个

    private static final DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofPattern("dd-MM-uuuu");
    

    格式化你的LocalDate 就可以了

    yourLocalDate.format(dateFormatter);
    

    或者如果您还没有机会转换 Date,那么当然可以

    yourDate.toLocalDate().format(dateFormatter);
    

    【讨论】:

      【解决方案2】:

      您可以在检索时格式化日期:

      Select Format(for_date, 'dd/mm/yyyy') as FOR_DATE,outage_time as OUTAGE_TIME,stat_detail as STATION_DETAILS from " + table_sel + " where for_date='" + date1 + "' and stat_detail ='" + combo_sel + "'" 
      

      【讨论】:

      • 嗨,布莱恩!我尝试了你的建议,但不知何故它给了我一个语法错误。 (; 预期的)不知道如何解决这个问题。
      • 好的,我终于在你的帮助下想出了如何显示它。 dd/mm/yyyy 应该在单引号内!选择 Format(for_date, 'dd/mm/yyyy') 作为 FOR_DATE,outage_time 作为 OUTAGE_TIME,stat_detail 作为 STATION_DETAILS from " + table_sel + " where for_date='" + date1 + "' and stat_detail ='" + combo_sel + "'"
      • 不要依赖数据库进行转换。使用渲染。
      【解决方案3】:

      您可以使用JTable#setDefaultRenderer 替换该列的TableCellRenderer。要在渲染器中格式化日期,您可以使用 SimpleDateFormat

      【讨论】:

        【解决方案4】:

        您可以尝试使用简单的日期格式将一种格式更改为另一种格式吗

        String date_s = "2011-01-18 00:00:00.0";
        
            SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date date = dt.parse(date_s);
        
        
            SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println(dt1.format(date));
        

        【讨论】:

        • 你好!当我在 jTable 中显示此代码时,我需要将它放在哪里。你能告诉我吗?
        • 你能不能试试这个 public class DateCellRenderer extends DefaultTableCellRenderer { public DateCellRenderer() { super(); } @Override public void setValue(final Object value) { final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); strValue = sdf.format(值); } super.setText(strValue); } }
        • @gatisahu,不要将代码放在评论中。正如您所注意到的,代码不可读。你的答案总是可以编辑的。也不要在渲染器代码中创建 SimpleDateFormat 对象,因为此方法是连续调用的。如我的回答所示,在类的实例上创建 SimpleDataFormat。
        【解决方案5】:

        您需要提供自定义渲染器来格式化日期。阅读 Concepts: Editors and Renderers 上的 Swing 教程中的部分,了解有关使用带有表格的渲染器的基本信息。

        一个基本的渲染器是:

        public class YMDRenderer extends DefaultTableCellRenderer
        {
            private Format formatter = new SimpleDateFormat("yy/MM/dd");
        
            public void setValue(Object value)
            {
                //  Format the Object before setting its value in the renderer
        
                try
                {
                    if (value != null)
                        value = formatter.format(value);
                }
                catch(IllegalArgumentException e) {}
        
                super.setValue(value);
            }
        }
        

        此外,请查看 Table Format Renderers 了解可重用类,只需提供 FormatRenderer,即可轻松确定日期、时间和编号。

        【讨论】:

          猜你喜欢
          • 2019-08-23
          • 1970-01-01
          • 1970-01-01
          • 2018-05-08
          • 2016-05-12
          • 2021-05-08
          • 1970-01-01
          • 2021-10-05
          • 2021-05-24
          相关资源
          最近更新 更多