JodaDateTimeConverter.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.jaxrs;

  21. import javax.ws.rs.WebApplicationException;
  22. import javax.ws.rs.ext.ParamConverter;

  23. import org.joda.time.DateTimeZone;
  24. import org.joda.time.format.DateTimeFormatter;
  25. import org.joda.time.format.ISODateTimeFormat;
  26. import org.slf4j.Logger;

  27. /**
  28.  * JodaDateTimeConverter
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class JodaDateTimeConverter implements ParamConverter<org.joda.time.DateTime> {

  35.     private static final Logger log = org.slf4j.LoggerFactory.getLogger(JodaDateTimeConverter.class);

  36.     private DateTimeZone timeZone = null;
  37.     public JodaDateTimeConverter(DateTimeZone timeZone) {
  38.         this.timeZone = timeZone;  
  39.     }

  40.     @Override
  41.     public org.joda.time.DateTime fromString(String date) {
  42.         /*
  43.         try {
  44.             Date d = Utilities.parseDate(date);
  45.             return new org.joda.time.DateTime(d);
  46.         } catch (Exception ex) {
  47.             log.error(ex.getMessage(),ex);
  48.             throw new WebApplicationException("Converter '"+date+"' to org.joda.time.DateTime failed: "+ex,ex,400);
  49.         }
  50.         */
  51.         try {
  52.             //DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser();
  53.             DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(this.timeZone);
  54.             return formatter.parseDateTime(date);
  55.         } catch (Exception ex) {
  56.             log.error(ex.getMessage(),ex);
  57.             throw new WebApplicationException("Converter org.joda.time.DateTime to String failed: "+ex,ex,400);
  58.         }
  59.     }

  60.     @Override
  61.     public String toString(org.joda.time.DateTime t) {
  62.         try {
  63.             //DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser();
  64.             DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(this.timeZone);
  65.             return formatter.print(t);
  66.         } catch (Exception ex) {
  67.             log.error(ex.getMessage(),ex);
  68.             throw new WebApplicationException("Converter org.joda.time.DateTime to String failed: "+ex,ex,400);
  69.         }
  70.     }

  71. }