0

I have a date conversion random issue with a Java application. I use a class JaxbDateSerializer which extends XmlAdapter to decode a string date

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * Adapts a Date for custom marshaling.
 *
 */

public class JaxbDateSerializer extends XmlAdapter<String, Date> {
/**
 * le format yyyyMMddHHmmss
 */
public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");

/**
 * le format yyyyMMdd
 */
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");

@Override
public String marshal(Date date) {
    return DATE_TIME_FORMAT.format(date);
}

Another class is called to handle each line of this file and I compare date returned to test if it's on the future or not to trigger a warning

    /*
 * (non-Javadoc)
 * @see com.afone.rdn.traitement.TraitementTransverse#setActivite(int, java.util.Date, int)
 */
@Override
public void setActiviteNumero(ActiviteNumero activiteNumero) throws RdnTraitementException {

    LOGGER.debug("TraitementTransverseImpl setActivite pour " + activiteNumero.getNumero() + " >> ("
            + activiteNumero.getDate() + ") traffic entrant: " + activiteNumero.getEntrantSortant());

    Timestamp time = null;
    if (activiteNumero.getDate() != null) {
        time = new Timestamp(activiteNumero.getDate().getTime());
    }
    com.afone.rdn.entities.Numero numeroActivite = new com.afone.rdn.entities.Numero(activiteNumero.getNumero(), time,
            activiteNumero.getEntrantSortant());
    try {
        com.afone.rdn.entities.Numero numeroTmp = numeroDAO.getNumero(numeroActivite.getNumero());
        if (!time.after(new Date((new java.util.Date()).getTime()))) {
            if (null == numeroTmp) {
                numeroDAO.mettreAJourNumero(numeroActivite);
            } else if ((null != numeroTmp.getDateDernierTrafic())
                    && (numeroTmp.getDateDernierTrafic().before(numeroActivite.getDateDernierTrafic()))) {
                numeroDAO.mettreAJourNumero(numeroActivite);
            }
        } else {
            LOGGER.warn("TraitementTransverseImpl setActivite date incorrecte numero: " + activiteNumero.getNumero()
                    + " date " + activiteNumero.getDate());
        }
    } catch (RdnTechniqueDaoException daoE) {
        throw new RdnTechniqueTraitementException(daoE);
    }
}

Camel get the CSV file and handle each line but we are getting some random errors about invalid dates like this one:

2015-04-09 00:34:43,906 WARN [TraitementTransverseImpl:setActiviteNumero] TraitementTransverseImpl setActivite date incorrecte numero: 299999999 date Fri Aug 28 11:06:28 CEST 2015

I don't know how to find the problem.

Regards, Ludovic

1 Answers1

0

The most likely reason for this behaviour is because of these lines:

public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
...
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");

java.text.DateFormat objects are not thread safe.

You must create new instances of these local to your marshall method.

@Override
public String marshal(Date date) {
    return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
}
Steve C
  • 18,876
  • 5
  • 34
  • 37