Program: 65989
Project: 65989
Buggy source:
/** 
 * This method returns a string representing a given Timestamp based on a given schema type QName. BC dates (sourceDate.getTime() < YEAR_ONE_AD_TIME) are handled as follows: '2007 BC' --> '-2006 AD'.
 * @param sourceDate
 * @param schemaType
 * @return
 */
private String stringFromTimestamp(Timestamp sourceDate,QName schemaType){
  if (XMLConstants.DATE_QNAME.equals(schemaType)) {
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {
      xgc.setYear(-cal.get(Calendar.YEAR));
    }
 else {
      xgc.setYear(cal.get(Calendar.YEAR));
    }
    xgc.setMonth(cal.get(Calendar.MONTH) + 1);
    xgc.setDay(cal.get(Calendar.DAY_OF_MONTH));
    return xgc.toXMLFormat();
  }
  if (XMLConstants.TIME_QNAME.equals(schemaType)) {
    Calendar cal=Calendar.getInstance(getTimeZone());
    cal.setTimeInMillis(sourceDate.getTime());
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    xgc.setHour(cal.get(Calendar.HOUR_OF_DAY));
    xgc.setMinute(cal.get(Calendar.MINUTE));
    xgc.setSecond(cal.get(Calendar.SECOND));
    String string=xgc.toXMLFormat();
    string=appendNanos(string,sourceDate);
    return appendTimeZone(string,sourceDate);
  }
  if (XMLConstants.G_DAY_QNAME.equals(schemaType)) {
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    xgc.setDay(cal.get(Calendar.DAY_OF_MONTH));
    return xgc.toXMLFormat();
  }
  if (XMLConstants.G_MONTH_QNAME.equals(schemaType)) {
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    xgc.setMonth(cal.get(Calendar.MONTH) + 1);
    String xmlFormat=xgc.toXMLFormat();
    if (xmlFormat.length() == 6) {
      xmlFormat=xmlFormat.substring(0,4);
    }
    return xmlFormat;
  }
  if (XMLConstants.G_MONTH_DAY_QNAME.equals(schemaType)) {
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    xgc.setMonth(cal.get(Calendar.MONTH) + 1);
    xgc.setDay(cal.get(Calendar.DAY_OF_MONTH));
    return xgc.toXMLFormat();
  }
  if (XMLConstants.G_YEAR_QNAME.equals(schemaType)) {
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {
      xgc.setYear(-cal.get(Calendar.YEAR));
    }
 else {
      xgc.setYear(cal.get(Calendar.YEAR));
    }
    return xgc.toXMLFormat();
  }
  if (XMLConstants.G_YEAR_MONTH_QNAME.equals(schemaType)) {
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {
      xgc.setYear(-cal.get(Calendar.YEAR));
    }
 else {
      xgc.setYear(cal.get(Calendar.YEAR));
    }
    xgc.setMonth(cal.get(Calendar.MONTH) + 1);
    return xgc.toXMLFormat();
  }
  if (XMLConstants.DURATION_QNAME.equals(schemaType)) {
    throw new IllegalArgumentException();
  }
  XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
  GregorianCalendar cal=new GregorianCalendar(getTimeZone());
  cal.setGregorianChange(new Date(Long.MIN_VALUE));
  cal.setTime(sourceDate);
  if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {
    xgc.setYear(-cal.get(Calendar.YEAR));
  }
 else {
    xgc.setYear(cal.get(Calendar.YEAR));
  }
  xgc.setMonth(cal.get(Calendar.MONTH) + 1);
  xgc.setDay(cal.get(Calendar.DAY_OF_MONTH));
  xgc.setHour(cal.get(Calendar.HOUR_OF_DAY));
  xgc.setMinute(cal.get(Calendar.MINUTE));
  xgc.setSecond(cal.get(Calendar.SECOND));
  String string=xgc.toXMLFormat();
  string=appendNanos(string,sourceDate);
  return appendTimeZone(string,sourceDate);
}

Fixed version (ground truth):
/** 
 * This method returns a string representing a given Timestamp based on a given schema type QName. BC dates (sourceDate.getTime() < YEAR_ONE_AD_TIME) are handled as follows: '2007 BC' --> '-2006 AD'.
 * @param sourceDate
 * @param schemaType
 * @return
 */
private String stringFromTimestamp(Timestamp sourceDate,QName schemaType){
  if (XMLConstants.DATE_QNAME.equals(schemaType)) {
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {
      xgc.setYear(-cal.get(Calendar.YEAR));
    }
 else {
      xgc.setYear(cal.get(Calendar.YEAR));
    }
    xgc.setMonth(cal.get(Calendar.MONTH) + 1);
    xgc.setDay(cal.get(Calendar.DAY_OF_MONTH));
    return xgc.toXMLFormat();
  }
  if (XMLConstants.TIME_QNAME.equals(schemaType)) {
    Calendar cal=Calendar.getInstance(getTimeZone());
    cal.setTimeInMillis(sourceDate.getTime());
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    xgc.setHour(cal.get(Calendar.HOUR_OF_DAY));
    xgc.setMinute(cal.get(Calendar.MINUTE));
    xgc.setSecond(cal.get(Calendar.SECOND));
    String string=xgc.toXMLFormat();
    string=appendNanos(string,sourceDate);
    return appendTimeZone(string,sourceDate);
  }
  if (XMLConstants.G_DAY_QNAME.equals(schemaType)) {
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    xgc.setDay(cal.get(Calendar.DAY_OF_MONTH));
    return xgc.toXMLFormat();
  }
  if (XMLConstants.G_MONTH_QNAME.equals(schemaType)) {
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    xgc.setMonth(cal.get(Calendar.MONTH) + 1);
    String xmlFormat=xgc.toXMLFormat();
    if (trimGMonth() && xmlFormat.length() == 6) {
      xmlFormat=xmlFormat.substring(0,4);
    }
    return xmlFormat;
  }
  if (XMLConstants.G_MONTH_DAY_QNAME.equals(schemaType)) {
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    xgc.setMonth(cal.get(Calendar.MONTH) + 1);
    xgc.setDay(cal.get(Calendar.DAY_OF_MONTH));
    return xgc.toXMLFormat();
  }
  if (XMLConstants.G_YEAR_QNAME.equals(schemaType)) {
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {
      xgc.setYear(-cal.get(Calendar.YEAR));
    }
 else {
      xgc.setYear(cal.get(Calendar.YEAR));
    }
    return xgc.toXMLFormat();
  }
  if (XMLConstants.G_YEAR_MONTH_QNAME.equals(schemaType)) {
    XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
    GregorianCalendar cal=new GregorianCalendar(getTimeZone());
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.setTime(sourceDate);
    if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {
      xgc.setYear(-cal.get(Calendar.YEAR));
    }
 else {
      xgc.setYear(cal.get(Calendar.YEAR));
    }
    xgc.setMonth(cal.get(Calendar.MONTH) + 1);
    return xgc.toXMLFormat();
  }
  if (XMLConstants.DURATION_QNAME.equals(schemaType)) {
    throw new IllegalArgumentException();
  }
  XMLGregorianCalendar xgc=getDatatypeFactory().newXMLGregorianCalendar();
  GregorianCalendar cal=new GregorianCalendar(getTimeZone());
  cal.setGregorianChange(new Date(Long.MIN_VALUE));
  cal.setTime(sourceDate);
  if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {
    xgc.setYear(-cal.get(Calendar.YEAR));
  }
 else {
    xgc.setYear(cal.get(Calendar.YEAR));
  }
  xgc.setMonth(cal.get(Calendar.MONTH) + 1);
  xgc.setDay(cal.get(Calendar.DAY_OF_MONTH));
  xgc.setHour(cal.get(Calendar.HOUR_OF_DAY));
  xgc.setMinute(cal.get(Calendar.MINUTE));
  xgc.setSecond(cal.get(Calendar.SECOND));
  String string=xgc.toXMLFormat();
  string=appendNanos(string,sourceDate);
  return appendTimeZone(string,sourceDate);
}