在jTable中动态解析日期 [英] Parsing Date dynamically in a jTable

查看:131
本文介绍了在jTable中动态解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含四列的jTable.第一列保存一些成员代码,第二列保存其准入日期,第三列保存其有效期.如果到期日期距离系统日期超过10天,则必须从列中删除行.

I have a jTable with four columns. the first column holds some code of members, second column holds their admission date and third column holds their validation expiry date. I've to remove the rows from the column if the expiry date is more than 10 days from the system date.

我正在写这样的代码:

    public void expire(){

    try{
    DefaultTableModel model = (DefaultTableModel) empTbl.getModel();
    int col=2;
    int rows = empTbl.getRowCount();
    for(int row=0; row<=rows; row++){

     SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy");
     Calendar currentDate = Calendar.getInstance();
     String d = formater.format(currentDate.getTime());
     Date haatdinPisorTarikh = (Date)formater.parse(d);   

     String expdate = (String)empTbl.getValueAt(row, col);
     Date expire=(Date)formater.parse(expdate);
     Calendar expireDate = Calendar.getInstance();
     expireDate.add(Calendar.DATE, -10);

     if(expireDate.after(haatdinPisorTarikh)){
     model.removeRow(row);
     }
     }

      }catch(ParseException ex){}
       }

此方法无效.我在哪里做错了? 请帮忙...

This method is not working. Where am I doing mistake ? Please help...

推荐答案

您可以从头开始进行整理.

You can organize the for starting from the end.

int rows = empTbl.getRowCount(); 
for(int row=rows-1; row>=0; row--) {
    //delete from model won't harm your row indexes.
}

这篇关于在jTable中动态解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆