JodaLocalDateConverter.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.LocalDate;
  25. import org.joda.time.format.DateTimeFormatter;
  26. import org.joda.time.format.ISODateTimeFormat;
  27. import org.slf4j.Logger;

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

  36.     private static final Logger log = org.slf4j.LoggerFactory.getLogger(JodaLocalDateConverter.class);

  37.     private DateTimeZone timeZone = null;
  38.     public JodaLocalDateConverter(DateTimeZone timeZone) {
  39.         this.timeZone = timeZone;  
  40.     }
  41.    
  42.     @Override
  43.     public org.joda.time.LocalDate fromString(String date) {
  44.         try {
  45.             //DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser();
  46.             DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(this.timeZone);
  47.             LocalDate ld = formatter.parseLocalDate(date);
  48.             return ld;
  49.         } catch (Exception ex) {
  50.             log.error(ex.getMessage(),ex);
  51.             throw new WebApplicationException("Converter '"+date+"' to org.joda.time.DateTime failed: "+ex,ex,400);
  52.         }
  53.     }

  54.     @Override
  55.     public String toString(org.joda.time.LocalDate t) {
  56.          try {
  57.              // DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser();
  58.              DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(this.timeZone);
  59.              return formatter.print(t);
  60.          } catch (Exception ex) {
  61.             log.error(ex.getMessage(),ex);
  62.             throw new WebApplicationException("Converter org.joda.time.DateTime to String failed: "+ex,ex,400);
  63.          }
  64.     }

  65. }