blob: 04314c900ec88257685a5da09b2b4cec96c10669 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2013-2020 LAAS-CNRS (www.laas.fr)
* 7 Colonel Roche 31077 Toulouse - France
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* Initial Contributors:
* Thierry Monteil : Project manager, technical co-manager
* Mahdi Ben Alaya : Technical co-manager
* Samir Medjiah : Technical co-manager
* Khalil Drira : Strategy expert
* Guillaume Garzone : Developer
* François Aïssaoui : Developer
*
* New contributors :
*******************************************************************************/
package org.eclipse.om2m.commons.utils;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Converts {@link XMLGregorianCalendar} to {@link Date} and inverse i.e.
*/
public class DateConverter {
/** Logger */
private static Log LOGGER = LogFactory.getLog(DateConverter.class);
/**
* Converts java.util.Date to javax.xml.datatype.XMLGregorianCalendar
* @param date - The Date to convert
* @return xmlGregorianCalendar object
*/
public static XMLGregorianCalendar toXMLGregorianCalendar(Date date) {
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(date);
XMLGregorianCalendar xmlGregorianCalendar = null;
try {
xmlGregorianCalendar = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(gregorianCalendar);
} catch (DatatypeConfigurationException e) {
LOGGER.error("Date to XMLGregorianCalendar error",e);
}
return xmlGregorianCalendar;
}
}