Package KFM.DateTimeServices

Source Code of KFM.DateTimeServices.KFM_DateTimeService

/*
*  This software and supporting documentation were developed by
*
*    Siemens Corporate Technology
*    Competence Center Knowledge Management and Business Transformation
*    D-81730 Munich, Germany
*
*    Authors (representing a really great team ;-) )
*            Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
*  This software is Open Source under GNU General Public License (GPL).
*  Read the text of this license in LICENSE.TXT
*  or look at www.opensource.org/licenses/
*
*  Once more we emphasize, that:
*  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  WITHOUT ANY WARRANTY
*  REGARDING  THE  SOFTWARE,  ITS  PERFORMANCE OR
*  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
*  ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
*  PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/


//  KFM_DateTimeService

// ************ package ******************************************************
package KFM.DateTimeServices;

// ************ imports ******************************************************

// KFM packages
import KFM.Exceptions.ProgrammerException;

// java packages
import java.lang.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;

/** KFM_DateTimeService gives access to KFM's dates and times.
*
* an instance of KFM_DateTimeService delivers the following services to the user:
* <BR> - current date as KFM_Date
* <BR> - current time as KFM_Time
* <BR> - current date and time as KFM_DateTime
*
* <BR> Later on there could be some extra utilities:
* <BR> - some functions for comparing KFM_Dates, KFM_Times and KFM_DateTimes
* <BR> - some functions for converting KFM_Dates, KFM_Times and KFM_DateTimes
*        in Java-Date objects and vice versa
* <BR> will be inserted when needed!
* <BR> <BR>
*
* Usage and aspects of class: <BR>
* - the user can set the timezone in the constructor. values from -12 to +12 are allowed. <BR>
* - the default constructor sets a timezone suited for Germany. <BR>
* - summertime savings are set if wished. (There is only one rule: 1 hour between last sunday
*   of March and last sunday of October!)<BR>
* - class is threadsafe.  <BR>
*   (... or will be in some time!)
*
* @version 0.1 (98 04 24)
*/
public class KFM_DateTimeService
{
   private Calendar calendar = null;

   /** Constructs a KFM_DateTimeService with timezone gmt + 1, (e.g. for Germany).
    */
   public KFM_DateTimeService ()
   {
      calendar = getCalendar(1, true);
   }

   /** Constructs a KFM_DateTimeService with timezone offset.
    *
    * `offset� should be a value between -12 and +12.
    * otherwise the default timezone will be choosen.
    * This will contain NO summertime savings!!!
    */
   public KFM_DateTimeService (int offset)
   {
      calendar = getCalendar(offset, false);
   }

   /** Constructs a KFM_DateTimeService with timezone offset.
    *
    *  @param offset        the offset in hours to GMT
    *  @param summertime    true: with summertime savings
    *                       false: no summertime savings
    */
   public KFM_DateTimeService (int offset, boolean summertime)
   {
      calendar = getCalendar(offset, summertime);
   }

   /** Gets a calendar with timezone offset.
    *
    *  offset should be a value between -12 and +12.
    *  otherwise the default timezone GMT + 1 will be choosen.
    */
    private Calendar getCalendar (int offset, boolean summertime)
    {
        Calendar calendar;
        SimpleTimeZone timeZone;

        // for wrong offsets we use default offset 1
        if(offset < -12 || offset > 12)
            offset = 1;

        // get the supported ids for the
        String[] ids = TimeZone.getAvailableIDs(offset * 60 * 60 * 1000);
        // if no ids were returned, something is wrong. get out.
        if(ids.length == 0) {
            //@@@ System.exit(0);
            //@@@ Ouch! Quick hack, make better some time.
            throw new ProgrammerException("KFM_DateTimeService.getCalendar: "
                + "TimeZone.getAvailableIDs did not return any IDs.");
        }

      timeZone = new SimpleTimeZone(offset * 60 * 60 * 1000, ids[0]);
      if(summertime) {
         timeZone.setStartRule(Calendar.MARCH, -1, Calendar.SUNDAY, 1 * 60 * 60 * 1000);
         timeZone.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 1 * 60 * 60 * 1000);
      }

      calendar = Calendar.getInstance(timeZone);

      return calendar;
   }

   /** Get the current Date as KFM_Date
    */
   public synchronized KFM_Date getCurrentDate()
   {
      calendar.setTime(new Date());
      return (new KFM_Date(calendar.get(Calendar.YEAR),
                           calendar.get(Calendar.MONTH) + 1,
                           calendar.get(Calendar.DAY_OF_MONTH)));
   }

   /** Get a specific Date as KFM_Date
    */
   public synchronized KFM_Date getDate(Date specificDate)
   {
      calendar.setTime(specificDate);
      return (new KFM_Date(calendar.get(Calendar.YEAR),
                           calendar.get(Calendar.MONTH) + 1,
                           calendar.get(Calendar.DAY_OF_MONTH)));
   }

   /** Get the current time as KFM_Time.
    *
    *  hours are given as 24h-values.
    */
   public synchronized KFM_Time getCurrentTime()
   {
      calendar.setTime(new Date());
      return (new KFM_Time(calendar.get(Calendar.HOUR_OF_DAY),
                           calendar.get(Calendar.MINUTE),
                           calendar.get(Calendar.SECOND)));
   }

   /** Get a given time as KFM_Time.
    *
    *  hours are given as 24h-values.
    */
   public synchronized KFM_Time getTime(Date specificDate)
   {
      calendar.setTime(specificDate);
      return (new KFM_Time(calendar.get(Calendar.HOUR_OF_DAY),
                           calendar.get(Calendar.MINUTE),
                           calendar.get(Calendar.SECOND)));
   }

   /** Get the current date and time as KFM_DateTime.
    *
    *  hours are given as 24h-values.
    */
   public synchronized KFM_DateTime getCurrentDateTime()
   {
      calendar.setTime(new Date());
      return (new KFM_DateTime(calendar.get(Calendar.YEAR),
                               calendar.get(Calendar.MONTH) + 1,
                               calendar.get(Calendar.DAY_OF_MONTH),
                               calendar.get(Calendar.HOUR_OF_DAY),
                               calendar.get(Calendar.MINUTE),
                               calendar.get(Calendar.SECOND)));
   }

   /**
    *  get a specific date and time as KFM_DateTime.
    *
    *  hours are given as 24h-values.
    */
   public synchronized KFM_DateTime getDateTime(Date specificDate)
   {
      calendar.setTime(specificDate);
      return (new KFM_DateTime(calendar.get(Calendar.YEAR),
                               calendar.get(Calendar.MONTH) + 1,
                               calendar.get(Calendar.DAY_OF_MONTH),
                               calendar.get(Calendar.HOUR_OF_DAY),
                               calendar.get(Calendar.MINUTE),
                               calendar.get(Calendar.SECOND)));
   }

    /** This Format String is used for createTimeStamp() */
    public static final String cTimeStampFormat =
        "yyyy-MM-dd hh:mm:ss";
    private static final SimpleDateFormat cSDFormat =
        new SimpleDateFormat(cTimeStampFormat);

    /**
     * Creates a current timestamp using the KFM format.
     * The format is described by cTimeStampFormat.
     *
     * @return String
     * @see #cTimeStampFormat
     */
    public synchronized static String createTimeStamp()
    {
        return cSDFormat.format(new Date());
    }

    /** This Format String is used for createTimeStampGerman() */
    public static final String cTimeStampFormatDE =
        "dd-MM-yyyy hh:mm:ss";
    private static final SimpleDateFormat cSDFormatDE =
        new SimpleDateFormat(cTimeStampFormatDE);

    /**
     * Creates a current timestamp using the German KFM format.
     * The format is described by cTimeStampFormatDE.
     * This used to be KFM.GUI.Page createTimeStamp().
     *
     * @return String
     * @see #cTimeStampFormatDE
     */
    public synchronized static String createTimeStampGerman()
    {
        return cSDFormatDE.format(new Date());
    }

   /** Closes the service.
    *
    *  removes the reference to the calendar for garbage collection
    */
   public void close() {
      calendar = null;
   }
}
TOP

Related Classes of KFM.DateTimeServices.KFM_DateTimeService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.