DateTimeFormatterWrapper.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.date;

  21. import java.text.FieldPosition;
  22. import java.text.ParseException;
  23. import java.text.SimpleDateFormat;
  24. import java.util.Calendar;
  25. import java.util.Date;
  26. import java.util.TimeZone;

  27. /**
  28.  * DateTimeFormatterWrapper
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class DateTimeFormatterWrapper extends SimpleDateFormat {

  35.     /**
  36.      *
  37.      */
  38.     private static final long serialVersionUID = 1L;
  39.    
  40.     private DateType dataType;
  41.     private String format;
  42.     private boolean timeZone;

  43.     public DateTimeFormatterWrapper(DateType dataType, String format, boolean timeZone) {
  44.         this.dataType = dataType;
  45.         this.format = format;
  46.         this.timeZone = timeZone;
  47.     }
  48.    
  49.     private TimeZone zone;
  50.     @Override
  51.     public void setTimeZone(TimeZone zone) {
  52.         this.zone = zone;
  53.     }
  54.    
  55.     @Override
  56.     public Date parse(String source) throws ParseException {
  57.        
  58.         try {
  59.             switch (this.dataType) {
  60.             case JAVA_UTIL_DATE:
  61.             case JAVA_UTIL_DATE_TIME:
  62.             case JAVA_UTIL_TIME:
  63.                 SimpleDateFormat sdf =  new SimpleDateFormat (this.format); // SimpleDateFormat non e' thread-safe
  64.                 if(this.timeZone) {
  65.                     sdf.setCalendar(Calendar.getInstance());
  66.                 }
  67.                 else if(this.zone!=null) {
  68.                     sdf.setTimeZone(this.zone);
  69.                 }
  70.                 return sdf.parse(source);
  71.                
  72.             case JAVA_TIME_DATE:
  73.                 return DateUtils.convertToDateViaInstant( DateUtils.parseToLocalDate(this.format, source) );
  74.             case JAVA_TIME_DATE_TIME:
  75.                 return DateUtils.convertToDateViaInstant( DateUtils.parseToLocalDateTime(this.format, source) );
  76.             case JAVA_TIME_TIME:
  77.                 return DateUtils.convertToDateViaInstant( DateUtils.parseToLocalTime(this.format, source) );
  78.            
  79.             case JODA_DATE:
  80.             case JODA_DATE_TIME:
  81.             case JODA_TIME:
  82.                 return DateUtils.convertToDate(DateUtils.parseToJodaDateTime(this.format, source));
  83.                
  84.             }
  85.         }catch(Exception e) {
  86.             throw new RuntimeException("["+this.dataType+"] "+e.getMessage(),e);
  87.         }
  88.        
  89.         throw new RuntimeException("["+this.dataType+"] unsupported");
  90.     }

  91.     @Override
  92.     public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) {
  93.        
  94.         try {
  95.             switch (this.dataType) {
  96.             case JAVA_UTIL_DATE:
  97.             case JAVA_UTIL_DATE_TIME:
  98.             case JAVA_UTIL_TIME:
  99.                 SimpleDateFormat sdf =  new SimpleDateFormat (this.format); // SimpleDateFormat non e' thread-safe
  100.                 if(this.timeZone) {
  101.                     sdf.setCalendar(Calendar.getInstance());
  102.                 }
  103.                 else if(this.zone!=null) {
  104.                     sdf.setTimeZone(this.zone);
  105.                 }
  106.                 toAppendTo.append(sdf.format(date));
  107.                 return toAppendTo;
  108.                
  109.             case JAVA_TIME_DATE:
  110.             case JAVA_TIME_DATE_TIME:
  111.             case JAVA_TIME_TIME:
  112.                 toAppendTo.append(DateUtils.getDateTimeFormatter(this.format).format(DateUtils.convertToZonedDateTimeViaInstant(date)));
  113.                 return toAppendTo;
  114.            
  115.             case JODA_DATE:
  116.             case JODA_DATE_TIME:
  117.             case JODA_TIME:
  118.                 toAppendTo.append(DateUtils.convertToJodaDateTime(date).toString(DateUtils.getJodaDateTimeFormatter(this.format)));
  119.                 return toAppendTo;
  120.             }
  121.         }catch(Exception e) {
  122.             throw new RuntimeException("["+this.dataType+"] "+e.getMessage(),e);
  123.         }
  124.        
  125.         throw new RuntimeException("["+this.dataType+"] unsupported");
  126.     }
  127.    
  128. }