public String encode() {
        if (encodedValue != null)
            return encodedValue;

        // Length is variable
        StringBuffer buf = new StringBuffer(10);

        if (negative)
            buf.append('-');
        buf.append('P');
        if ((years != 0) || (months == 0)) {
            buf.append(Long.toString(years));
            buf.append('Y');
        }
        if (months != 0) {
            buf.append(Long.toString(months));
            buf.append('M');
        }

        encodedValue = buf.toString();

        return encodedValue;
    }