All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.datetime.ThreadSafeDateFormat Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.datetime;

import java.text.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

//------------------------------------------------------------------------------
/**
 * Thread-safe extension of SimpleDateFormat
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class ThreadSafeDateFormat extends SimpleDateFormat
{
   ThreadLocal mDateFormat;

   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //--------------------------------------------------------------------------
   public ThreadSafeDateFormat()
   {
      super();
      mDateFormat = new ThreadLocal()
      {
         protected SimpleDateFormat initialValue()
         {
            return new SimpleDateFormat();
         }
      };
   }

   //--------------------------------------------------------------------------
   public ThreadSafeDateFormat(final String inPattern)
   {
      super();
      mDateFormat = new ThreadLocal()
      {
         protected SimpleDateFormat initialValue()
         {
            return new SimpleDateFormat(inPattern);
         }
      };
   }

   //--------------------------------------------------------------------------
   public ThreadSafeDateFormat(final String inPattern, final DateFormatSymbols formatSymbols)
   {
      super();
      mDateFormat = new ThreadLocal()
      {
         protected SimpleDateFormat initialValue()
         {
            return new SimpleDateFormat(inPattern, formatSymbols);
         }
      };
   }

   //--------------------------------------------------------------------------
   public ThreadSafeDateFormat(final String inPattern, final Locale locale)
   {
      super();
      mDateFormat = new ThreadLocal()
      {
         protected SimpleDateFormat initialValue()
         {
            return new SimpleDateFormat(inPattern, locale);
         }
      };
   }

   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //--------------------------------------------------------------------------
   @Override
   public Object parseObject(String source)
         throws ParseException
   {
      return mDateFormat.get().parseObject(source);
   }

   //--------------------------------------------------------------------------
   @Override
   public String toString()
   {
      return mDateFormat.get().toString();
   }

   //--------------------------------------------------------------------------
   @Override
   public Date parse(String source)
         throws ParseException
   {
      return mDateFormat.get().parse(source);
   }

   //--------------------------------------------------------------------------
   @Override
   public Object clone()
   {
      return mDateFormat.get().clone();
   }

   //--------------------------------------------------------------------------
   @Override
   public int hashCode()
   {
      return mDateFormat.get().hashCode();
   }

   //--------------------------------------------------------------------------
   @Override
   public boolean equals(Object obj)
   {
      return mDateFormat.get().equals(obj);
   }

   //--------------------------------------------------------------------------
   @Override
   public Object parseObject(String source, ParsePosition pos)
   {
      return mDateFormat.get().parseObject(source, pos);
   }

   //--------------------------------------------------------------------------
   @Override
   public void setCalendar(Calendar newCalendar)
   {
      mDateFormat.get().setCalendar(newCalendar);
   }

   //--------------------------------------------------------------------------
   @Override
   public Calendar getCalendar()
   {
      return mDateFormat.get().getCalendar();
   }

   //--------------------------------------------------------------------------
   @Override
   public void setNumberFormat(NumberFormat newNumberFormat)
   {
      mDateFormat.get().setNumberFormat(newNumberFormat);
   }

   //--------------------------------------------------------------------------
   @Override
   public NumberFormat getNumberFormat()
   {
      return mDateFormat.get().getNumberFormat();
   }

   //--------------------------------------------------------------------------
   @Override
   public void setTimeZone(TimeZone zone)
   {
      mDateFormat.get().setTimeZone(zone);
   }

   //--------------------------------------------------------------------------
   @Override
   public TimeZone getTimeZone()
   {
      return mDateFormat.get().getTimeZone();
   }

   //--------------------------------------------------------------------------
   @Override
   public void setLenient(boolean lenient)
   {
      mDateFormat.get().setLenient(lenient);
   }

   //--------------------------------------------------------------------------
   @Override
   public boolean isLenient()
   {
      return mDateFormat.get().isLenient();
   }

   //--------------------------------------------------------------------------
   @Override
   public void set2DigitYearStart(Date startDate)
   {
      mDateFormat.get().set2DigitYearStart(startDate);
   }

   //--------------------------------------------------------------------------
   @Override
   public Date get2DigitYearStart()
   {
      return mDateFormat.get().get2DigitYearStart();
   }

   //--------------------------------------------------------------------------
   @Override
   public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos)
   {
      return mDateFormat.get().format(date, toAppendTo, pos);
   }

   //--------------------------------------------------------------------------
   @Override
   public AttributedCharacterIterator formatToCharacterIterator(Object obj)
   {
      return mDateFormat.get().formatToCharacterIterator(obj);
   }

   //--------------------------------------------------------------------------
   @Override
   public Date parse(String text, ParsePosition pos)
   {
      return mDateFormat.get().parse(text, pos);
   }

   //--------------------------------------------------------------------------
   @Override
   public String toPattern()
   {
      return mDateFormat.get().toPattern();
   }

   //--------------------------------------------------------------------------
   @Override
   public String toLocalizedPattern()
   {
      return mDateFormat.get().toLocalizedPattern();
   }

   //--------------------------------------------------------------------------
   @Override
   public void applyPattern(String pattern)
   {
      mDateFormat.get().applyPattern(pattern);
   }

   //--------------------------------------------------------------------------
   @Override
   public void applyLocalizedPattern(String pattern)
   {
      mDateFormat.get().applyLocalizedPattern(pattern);
   }

   //--------------------------------------------------------------------------
   @Override
   public DateFormatSymbols getDateFormatSymbols()
   {
      return mDateFormat.get().getDateFormatSymbols();
   }

   //--------------------------------------------------------------------------
   @Override
   public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
   {
      mDateFormat.get().setDateFormatSymbols(newFormatSymbols);
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy