import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class CCExpire {
private static final DateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS");
private Calendar expiry = Calendar.getInstance();
public CCExpire(String month, String year)
throws IllegalArgumentException, NumberFormatException
{
if(year.length() != 4)
throw new IllegalArgumentException("year must be 4 digits");
/*
The card is valid up to and including the last day of the expiry
month, so set the expiry date as the first day of the next month.
Remember that months are 0-based so:
m = Integer.parseInt(month) - 1 // to make 0-based
+ 1 // to make next month
*/
int m = Integer.parseInt(month);
int y = Integer.parseInt(year);
expiry.set(Calendar.MONTH, m);
expiry.set(Calendar.YEAR, y);
expiry.set(Calendar.DATE, 1);
expiry.set(Calendar.HOUR_OF_DAY, 0);
expiry.set(Calendar.MINUTE, 0);
expiry.set(Calendar.SECOND, 0);
expiry.set(Calendar.MILLISECOND, 0);
synchronized(df) {
System.out.println("Expires: "+df.format(expiry.getTime()));
}
}
public boolean isExpired() {
return new Date().after(expiry.getTime());
}
public static void main(String[] args) {
CCExpire ccard = null;
if(args.length != 2)
System.exit(0);
try {
ccard = new CCExpire(args[0], args[1]);
synchronized(df) {
System.out.println(df.format(new Date())+" expired: "+ccard.isExpired());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}