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

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

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


import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.hfg.util.StringUtil;

//------------------------------------------------------------------------------
/**
 * Enumeration of standard time units.
 *
 * @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]
//------------------------------------------------------------------------------

@Deprecated
public class TimeUnit
{
   //**************************************************************************
   // PRIVATE FIELDS
   //**************************************************************************

   private String mName;
   private String mAbbrev;
   private int    mCalendarNum;
   private long   mMilliseconds;

   private static Map sUniqueMap = new HashMap();

   //**************************************************************************
   // PUBLIC FIELDS
   //**************************************************************************

   public static final TimeUnit MILLISECOND = new TimeUnit("millisecond",   "ms", Calendar.MILLISECOND,  1);
   public static final TimeUnit SECOND      = new TimeUnit(     "second",  "sec", Calendar.SECOND,       1000);
   public static final TimeUnit MINUTE      = new TimeUnit(     "minute",  "min", Calendar.MINUTE,       60 * TimeUnit.SECOND.getMilliseconds());
   public static final TimeUnit HOUR        = new TimeUnit(       "hour",   "hr", Calendar.HOUR_OF_DAY,  60 * TimeUnit.MINUTE.getMilliseconds());
   public static final TimeUnit DAY         = new TimeUnit(        "day",  "day", Calendar.DAY_OF_MONTH, 24 * TimeUnit.HOUR.getMilliseconds());
   public static final TimeUnit WEEK        = new TimeUnit(       "week",   "wk", Calendar.WEEK_OF_YEAR,  7 * TimeUnit.DAY.getMilliseconds());
   public static final TimeUnit MONTH       = new TimeUnit(      "month",  "mon", Calendar.MONTH,        (long)((365.25 * TimeUnit.DAY.getMilliseconds())/12));
   public static final TimeUnit YEAR        = new TimeUnit(       "year",   "yr", Calendar.YEAR,         (long)(365.25 * TimeUnit.DAY.getMilliseconds()));

   //**************************************************************************
   // CONSTRUCTORS
   //**************************************************************************

   //---------------------------------------------------------------------------
   private TimeUnit(String inName, String inAbbrev, int inCalendarNum, long inMilliseconds)
   {
      mName = inName;
      mAbbrev = inAbbrev;
      mCalendarNum = inCalendarNum;
      mMilliseconds = inMilliseconds;

      sUniqueMap.put(mName, this);
      sUniqueMap.put(mAbbrev, this);
   }

   //**************************************************************************
   // PUBLIC METHODS
   //**************************************************************************

   //---------------------------------------------------------------------------
   public static Collection values()
   {
      return sUniqueMap.values();
   }

   //---------------------------------------------------------------------------
   /**
    Returns the TimeUnit object that corresponds to the specified name or abbreviation string.
    @param inValue the string name or abbreviation value to be converted into a TimeUnit object
    @return the TimeUnit constant corresponding to the specified name or abbreviation
    */
   public static TimeUnit valueOf(String inValue)
   {
      TimeUnit result = null;
      if (StringUtil.isSet(inValue))
      {
         result = sUniqueMap.get(inValue.toLowerCase());
      }

      return result;
   }

   //---------------------------------------------------------------------------
   /**
    Returns the name associated with the TimeUnit.
    @return the name of the time unit
    */
   public String name()
   {
      return mName;
   }

   //---------------------------------------------------------------------------
   /**
    Returns the abbreviation associated with the TimeUnit.
    @return the abbreviation of the time unit
    */
   public String getAbbreviation()
   {
      return mAbbrev;
   }

   //---------------------------------------------------------------------------
   /**
    Returns the associated int value to use with a Calendar object's get() method.
    @return the Calendar object int value associated with this time unit.
    */
   public int getCalendarNum()
   {
      return mCalendarNum;
   }

   //---------------------------------------------------------------------------

   /**
    Returns the time unit's length in milliseconds. Note that the values for MONTH
    and YEAR are approximations.
    @return the length of time in milliseconds that corresponds to this unit of time.
    */
   public long getMilliseconds()
   {
      return mMilliseconds;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy