如何比较当前日期和Jtable中的给定日期?
问题描述:
美好的一天。 我有另一个与Jtable相关的问题。 如果列内的日期(到期日)超过或等于当前日期,我想更改表格的行颜色。如何比较当前日期和Jtable中的给定日期?
我想这个代码,但我得到一个错误:java.lang.NumberFormatException:对于输入字符串: “2012-03-15”
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String expDateString = sdf.format(cal.getTime());
System.out.println(expDateString);
Double date = Double.parseDouble(expDateString);
Double val = Double.parseDouble(tableSummary.getModel().getValueAt(row, 6).toString());
for(int i=0; i<=tableSummary.getRowCount()-1; i++){
if(val >= date){
renderer.setBackground(red);
}
}
谢谢!
这里有一个新的代码:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String expDateString = sdf.format(cal.getTime());
Date today = new Date(expDateString);
System.out.println("ang churva is " + today);
Date given = new Date(tableSummary.getModel().getValueAt(row, 6).toString());
for(int i=0; i<=tableSummary.getRowCount()-1; i++){
if(today.compareTo(given)>=0){
renderer.setBackground(red);
}
}
,但我得到这个异常:今天日期java.lang.IllegalArgumentException异常=新的日期(expDateString);
答
你不能在你怎么可以比较你日期的双
Double date = Double.parseDouble(expDateString); //does not work!
答
简单的例子蒙上了日期字符串。请注意,如果JTable中的对象已经是日期,则不需要全部解析,这会让您的生活更轻松。
下面的代码的输出是:
expiryDate=2012-03-15
tableDateOK=2012-03-12
tableDateExpired=2012-03-18
tableDateOK>expiryDate = false
tableDateExpired>expiryDate = true
代码:
public static void main(String args[]) throws ParseException {
String expiryDate = "2012-03-15";
String tableDateOk = "2012-03-12";
String tableDateExpired = "2012-03-18";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("expiryDate="+expiryDate);
System.out.println("tableDateOK="+tableDateOk);
System.out.println("tableDateExpired="+tableDateExpired);
System.out.println("tableDateOK>expiryDate = " + sdf.parse(tableDateOk).after(sdf.parse(expiryDate)));
System.out.println("tableDateExpired>expiryDate = " + sdf.parse(tableDateExpired).after(sdf.parse(expiryDate)));
}
答
线Double date = Double.parseDouble(expDateString);
这不可能,因为字符串 “2012-03-15” 的工作根本就不是一个有效的双重价值。
我不明白为什么您要比较两个double值:
- 如果您在表格日期,使用Date.after()和Date.before()找出来,不管你的约会现在是之前还是之后。
- ,如果你在表有字符串,请使用SimpleDateFormat.parse()从它那里得到的日期,并做点1
答
使用在结果集查询代码
DATEDIFF('d',NOW(),exdate)
。它会返回差异。改变它可能符合你的需求。
答
public String compareDate(Request request) throws ParseException {
Date debitDate= request.getPaymentTxn().getCrValDt();
Date now = new Date();
String response="";
SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");
String strCurrDate = sdfDate.format(now);
String strDebitDate = sdfDate.format(debitDate);
System.out.println("Current Date: " + strCurrDate);
Date currentDate = new SimpleDateFormat("dd/MM/yyyy").parse(strCurrDate);
Date txnDate = new SimpleDateFormat("dd/MM/yyyy").parse(strDebitDate);
System.out.println("C -> "+currentDate);
System.out.println("C -> "+txnDate);
if (txnDate!=null){
if (currentDate.equals(txnDate))
{
System.out.println("Valid Txn");
response="valid";
}
if (currentDate.after(txnDate))
{
System.out.println("--> Not Valid TXN Past");
response="notValid";
}
if (currentDate.before(txnDate)){
System.out.println("Future Valid TXn");
response="future";
}
}
return response;
}
请CHK出来其做工精细
可能重复:http://stackoverflow.com/questions/2592501/compare-dates-in-java – assylias 2012-03-15 11:27:03
您应当存储'Date'您'TableModel',在[检索](http://stackoverflow.com/q/9716893/230513)之后及[渲染]之前尽早将其转换(http://stackoverflow.com/q/9714110/230513) 。 – trashgod 2012-03-15 18:53:39