Reputation: 307
This is my code, and i have a simple question
import java.util.*;
import java.io.*;
import type.lib.GlobalCredit;
import type.lib.CreditCard;
import java.text.SimpleDateFormat;
public class eCheck08A
{
public static void main(String[] args)
{
PrintStream out = System.out;
Scanner in = new Scanner(System.in);
GlobalCredit credit1 = new GlobalCredit().getRandom();
out.print("Enter report range in years ... ");
int range = in.nextInt();
out.println("Cards expiring before " + range + " year(s) from now: ");
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
for (CreditCard cc : credit1)
{
Calendar c = Calendar.getInstance();
c.setTime(cc.getExpiryDate());
c.add(Calendar.YEAR, range);
Date newDate = c.getTime();
if (cc.getExpiryDate().compareTo(newDate) < range)
{
if(cc.getExpiryDate().compareTo(newDate) > range)
{
out.print("*");
}
out.print(cc.getNumber());
out.println("\t" + sf.format(cc.getExpiryDate()));
}
}
}
}
output of what it should look like:
Enter report range in years ... 3
Cards expiring before 3 years from now:
561561-8 20/11/2015
045645-7 22/02/2017
456462-3 16/04/2013 *
546548-5 19/08/2016
The current year is 2012 The person enters '3' as range. so any year that is from 2012-2015 should have a " * ". Like the output above, 2013 has a " * ". Can you tell what i am doing wrong in my IF statement?
Upvotes: 1
Views: 957
Reputation: 14363
Look at the java doc of Date.compareTo() method...
Returns: the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.
But this doesn't provide you the difference in years. It will give you only -1, 0 or 1.
As a solution you need to extract year from date and then make a comparison.
Upvotes: 0
Reputation: 347194
I think your entire logic is off. You should be comparing the credit card expiry Date based on date that is today + nYears
, not the expiryDate + nYears
.
Take a look at Date.after
, Date.equals
, Date.before
Upvotes: 1
Reputation: 143876
If you are comparing cc.getExpiryDate()
with the current date + the range, you want the newDate
to be:
Calendar c = Calendar.getInstance();
// commenting this line out because getInstance() gives us the current date already
// c.setTime(cc.getExpiryDate());
c.add(Calendar.YEAR, range);
Date newDate = c.getTime();
This newDate
is "range" years ahead of the current date. Now you can start comparing your cc.getExpiryDate()
values:
// expiry date is BEFORE the date + "range" years ahead
if (cc.getExpiryDate().compareTo(newDate) < 0)
{
// the expiry date is AFTER or ON the current date
if(cc.getExpiryDate().compareTo(new Date()) >= 0)
{
out.print("*");
}
}
out.print(cc.getNumber());
out.println("\t" + sf.format(cc.getExpiryDate()));
Upvotes: 1
Reputation: 70929
The compareTo method does not return what you expect. It is only guaranteed to return a negative if the first argument is less, positive if it is greater and zero if they are equal.
Edit: here is how you can change it so that your code works:
Date now = new Date(System.currentTimeMillis());
Date endDate = new Date(now.getTime());
endDate.SetYear(endDate.getYear() + 3);
if (cc.getExpiryDate().after(now) && cc.ExpiryDate.before(endDate)) {
// do stuff.
}
You will have to take care to handle edge cases(should you include ends of the interval and so on), but this should do as approach.
Upvotes: 1