Example: Year Fraction

The year fraction of a 30/360 year starting 8/1/85 and ending 7/1/86 is returned in this example.

import com.imsl.finance.*;
import java.text.*;
import java.util.*;

public class yearfracEx1 {

    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 start = parse("8/1/85");
        GregorianCalendar end = parse("7/1/86");
        DayCountBasis dcb = DayCountBasis.BasisNASD;
        double yearfrac = Bond.yearfrac(start, end, dcb);
        System.out.println("The year fraction of the 30/360 period is "
                + yearfrac);
    }
}

Output

The year fraction of the 30/360 period is 0.9166666666666667
Link to Java source.