Example: Modified Macauley Duration of a Security with Periodic Payments

The modified Macauley 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 mdurationEx1 {
    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 mduration = Bond.mduration(settlement, maturity,
        coupon, yield, freq, dcb);
        System.out.println("The modified Macauley duration of the bond" +
        "\nwith semiannual interest payments is " + mduration);
    }
}

Output

The modified Macauley duration of the bond
with semiannual interest payments is 6.738711366480527
Link to Java source.