On Java Development

All things related to Java development, from the perspective of a caveman.

Date and Time Helper Class

without comments

Introduction

This post introduces the methods of a helper class designed to retrieve system dates and times and supports formatting of dates and times from other sources. Helper classes are used to provide a specific function to assist the overall goal of the application and are different from so-called Utility classes.

The Utility pattern is a software pattern that is used for utility classes that do not require instantiation and only have static methods. The stateless class is designated as static so that no instance can be created. Good candidates for utility classes are convenience methods that can be grouped together functionally. Furthermore, methods in Utility classes are usually deterministic. As the Utility class is stateless, all parameters in each method must pass all necessary information to the method.

The DateTimeHelper class supports the retrieval and formatting of dates and times and is designed to supersede baseproject’s DateTimeFactory which is timezone oriented. Computing times across timezones is a hairball of a problem area. For example, Indian reservations do not observe DST. Arizona, normally in MST doesn’t observe DST, either. However, during DST, most of Arizona is observing the time as PST effectively shifting itself into another timezone. These as well as other factors have been incorporated into DateTimeFactory, but it is much more than what is needed. As a result DateTimeFactory has been deprecated and will be deleted from baseproject in the near future.

Deprecation is a status often assigned to methods and classes to warn the developer to stop using them as another method or class has been designed to replace it. Classes and methods which have been deprecated will appear in the IDE (Eclipse) with a line through its name as shown below.
DreprecatedClass

The methods of DateTimeHelper are shown below.

  • public Date getCurrentSystemDate() – Get a Date object from the current system time.
  • public int getCurrentSystemHHMMSS() – Get the current system time as integer in HHMMSS format
  • public String getCurrentSystemHHMMSSAsString() – Get the current system time as String in HHMMSS format
  • public int getCurrentSystemYYYYMMDD() – Get the current system date as integer in YYYYMMDD format.
  • public String getCurrentSystemYYYYMMDDAsString() -Get the current system date as String in YYYYMMDD format.
  • public String getFormattedDateOrTimeAsString(Date date, String pattern) – This method will format the supplied date object using the pattern supplied. Because the formatting may include the use of special characters (eg “/”, “:”) the value returned is a String. An example of its use is shown below.

DateTimeHelper incorporates the use of Java’s SimpleDateFormat class. As Oracle describes it,

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.

SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat. Each of these class methods can return a date/time formatter initialized with a default format pattern. You may modify the format pattern using the applyPattern methods as desired. For more information on using these methods, see DateFormat.

There are two methods as of this time that use SimpleDateFormat. These are getFormattedDateOrTimeAsString() and getFormattedDateOrTimeAsString(Date date, String pattern). Many patterns have been described in DateTimeHelper and are listed below. See the Java code above for an example of their use.

As can be seen the functions provided by DateTimeHelper are rather simple at this point. More date functionality will be added in the future, but it isn’t expected to grow in complexity such as it could be as described by this hapless developer.

Written by admin

January 23rd, 2014 at 6:01 pm

Leave a Reply

You must be logged in to post a comment.