Example: Duration of a Security with Periodic Payments

The annual duration of a 10 year bond which pays interest semiannually is returned in this example.
import com.imsl.finance.*;
import java.text.*;
import java.util.*;

public class durationEx1 {
    static final DateFormat dateFormat =
    DateFormat.getDateInstance(DateFormat.SHORT,  Locale.US);
    
    static private GregorianCalendar parse(String s) throws ParseException {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(dateFormat.parse(s));
        return cal;
    }
    
    public static void main(String args[]) throws ParseException {
        GregorianCalendar settlement = parse("7/1/85");
        GregorianCalendar maturity = parse("7/1/95");
        double coupon = .075;
        double yield = .09;
        int freq = Bond.SEMIANNUAL;
        DayCountBasis dcb = DayCountBasis.BasisActual365;
        double duration = Bond.duration(settlement, maturity, coupon,
        yield, freq, dcb);
        System.out.println("The annual duration of the bond with" +
        "\nsemiannual interest payments is " + duration);
    }
}

Output

The annual duration of the bond with
semiannual interest payments is 7.041953377972151
Link to Java source.