Example: Present Value of a Schedule of Cash Flows

In this example, the present value of 3 payments, $1,000, $2,000, and $1,000, with an interest rate of 5% made on January 3, 1997, January 3, 1999, and January 3, 2000 is computed.
import com.imsl.finance.*;
import java.text.*;
import java.util.*;

public class xnpvEx1 {
    static final DateFormat dateFormat = 
    DateFormat.getDateInstance(DateFormat.SHORT,  Locale.US);
    
    private static Date parse(String s)	throws ParseException {
        return dateFormat.parse(s);
    }
    
    public static void main(String args[]) throws ParseException {
        double rate = 0.05;
        double value[]	= {1000.,2000., 1000.};
        Date dates[] = {parse("1/3/1997"), parse("1/3/1999"), 
        parse("1/3/2000")};
        
        double pv = Finance.xnpv(rate, value, dates);
        System.out.println("The present value of the schedule of cash " + 
        "flows is " + NumberFormat.getCurrencyInstance().format(pv));
    }
}

Output

The present value of the schedule of cash flows is $3,677.90
Link to Java source.