Java 如何比较 Jtable 中的当前日期和给定日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9718691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Compare the Current Date and a given Date in a Jtable?
提问by zairahCS
Good Day. I've got another problem related to Jtable. I want to change the row color of a table if the date inside column (expiry) exceeds or is equal to the current date.
再会。我有另一个与 Jtable 相关的问题。如果列内的日期(到期)超过或等于当前日期,我想更改表格的行颜色。
I tried this code but i get an error: java.lang.NumberFormatException: For input string: "2012-03-15"
我试过这段代码,但我得到一个错误: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);
}
}
Thanks!
谢谢!
here's a new code:
这是一个新代码:
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);
}
}
but i get this exception: java.lang.IllegalArgumentException at Date today = new Date(expDateString);
但我得到这个异常: java.lang.IllegalArgumentException at Date today = new Date(expDateString);
回答by juergen d
You can't cast a date string in a double
您不能将日期字符串转换为双精度型
Double date = Double.parseDouble(expDateString); //does not work!
回答by assylias
Simple example of how you can compare you dates. Note that if the objects in your JTable already are Dates, you don't need all the parsing, which would make your life easier.
如何比较日期的简单示例。请注意,如果 JTable 中的对象已经是日期,则不需要所有解析,这将使您的生活更轻松。
The output of the code below is:
下面代码的输出是:
expiryDate=2012-03-15
tableDateOK=2012-03-12
tableDateExpired=2012-03-18
tableDateOK>expiryDate = false
tableDateExpired>expiryDate = true
expiryDate = 2012-03-15
tableDateOK = 2012-03-12
tableDateExpired = 2012-03-18
tableDateOK> expiryDate =假
tableDateExpired> expiryDate =真
Code:
代码:
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)));
}
回答by Jan Hruby
line Double date = Double.parseDouble(expDateString);
线 Double date = Double.parseDouble(expDateString);
this cannot work because string "2012-03-15" is simply not a valid double value.
这行不通,因为字符串“2012-03-15”根本不是有效的双精度值。
I do not understand why you are trying to compare two double values:
我不明白你为什么要比较两个双精度值:
- if you have Date in table, use Date.after() and Date.before() to find out, whether your date is before or after now.
- if you have String in table, use the SimpleDateFormat.parse() to get Date from it and do point 1
- 如果表中有日期,请使用 Date.after() 和 Date.before() 找出您的日期是在现在之前还是之后。
- 如果表中有字符串,请使用 SimpleDateFormat.parse() 从中获取日期并执行第 1 点
回答by Gomathi
Use the code
使用代码
DATEDIFF('d',NOW(),exdate)
in your resultset query. It will return the difference. Alter it possibly to match your needs.
在您的结果集查询中。它将返回差额。改变它可能符合您的需要。
回答by Vignesh Anand
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;
}
PLease Chk it out its working fine
请查清楚它的工作正常