Skip to content
Asif edited this page Apr 11, 2023 · 4 revisions

Utility or Helper class

What is Utility Class?

The utility class is helper. Let’s say we have a little logic in the code and we use this logic throughout the project, instead of writing this logic over and over in each class, we can create a new class and add this logic to the newly created class. And then we can call this logic wherever we want. Maybe we have 10 lines of code in our logic, we can replace this 10 lines of code with 1 line.

Why we need Utility Class?

Utility class helps us to improve code quality , readability. Also you can use Utility class to write good clean code. Other developers can understand your code easily. This is the main idea to use this class everywhere in the project. Now let’s see this Utility class in action.

Implement Utility class to our Project

In this base project we have written all utility Classes in com.fissionlab.coe.utils package

We have TimeUtilities.java Class which provides several static methods to handle date and time in UTC format.

Below is the explanation of Each Method in TimeUtilities

  1. public static Date getCurrentDateInUTC():

This method returns the current date and time in UTC time zone as a java.util.Date object. It creates a ZonedDateTime object representing the current date and time in UTC time zone using the ZonedDateTime.now() method with the ZoneOffset.UTC argument. Then it converts the ZonedDateTime object to a java.util.Date object using the Date.from() method.

  1. public static String getUTCDateInString():

This method returns the current date and time in UTC time zone as a String formatted with the pattern "yyyy-MM-dd HH:mm:ss". It creates a ZonedDateTime object representing the current date and time in UTC time zone using the ZonedDateTime.now() method with the ZoneOffset.UTC argument. Then it formats the ZonedDateTime object using a SimpleDateFormat object with the pattern "yyyy-MM-dd HH:mm:ss".

  1. public static String getUTCDateTimeInString():

This method returns the current date and time in UTC time zone as a String formatted with the pattern "yyyyMMddHHmmss". It creates a ZonedDateTime object representing the current date and time in UTC time zone using the ZonedDateTime.now() method with the ZoneOffset.UTC argument. Then it formats the ZonedDateTime object using a SimpleDateFormat object with the pattern "yyyyMMddHHmmss".

  1. public static String getUTCDateTimeInStringFormatted():

This method is similar to getUTCDateInString(), but it formats the output with a different pattern "yyyy-MM-dd HH:mm:ss".

  1. public static String getDateMMDDYYYY(Date date):

This method takes a java.util.Date object as input and returns it formatted as a String with the pattern "yyyy-MM-dd HH:mm:ss".

Below is the explanation of Each Method in FileUtils.java class

  1. public static String readFileAsString(String fileName)

reads the contents of a file and returns it as a String

  1. public static void writeStringToFile(String fileName, String content)

writes a String to a file, overwriting any existing file with the same name.

  1. public static void appendStringToFile(String fileName, String content)

appends a String to the end of a file, creating the file if it doesn't exist

  1. public static boolean deleteFile(String fileName)

returning true if the file was successfully deleted, and false otherwise

Below is the explanation of Each Method in Utility.java class

  1. public static boolean validateEmail(String email)

This method takes an email address as a string input and returns true if the input is a valid email address as per the defined regular expression pattern. It checks if the input string matches the VALID_EMAIL_ADDRESS_REGEX pattern, which uses a regular expression to match the email format.

  1. public static long getRandomLongValue()

This method generates a random long value between 100,000 and 999,999 and returns the generated value.

  1. public static String getStackTrace(Throwable t)

This method takes a Throwable object as input and returns a string containing the stack trace of the exception. It uses a StringWriter and a PrintWriter to capture the stack trace into a string.

  1. public static boolean checkIfStringConatinsNumber(String s)

This method checks if the input string contains any digit characters. It uses a regular expression to match the pattern.

  1. public static boolean checkIfStringConatinsAlphabets(String s)

This method checks if the input string contains any alphabetic characters. It uses a regular expression to match the pattern.

  1. public static boolean checkIfStringConatinsSpecialCharacters(String s)

This method checks if the input string contains any special characters (non-alphanumeric characters). It uses a regular expression to match the pattern.

  1. public static boolean checkIfNameIsValid(String s)

This method checks if the input string contains only alphabetic characters and spaces. It uses a regular expression to match the pattern

Below is the explanation of Each Method in Constants.java class

In this class we have defines a constant string UTC_DATE_FORMAT which specifies the format for representing dates and times in UTC (Coordinated Universal Time) as a string.

The format "yyyy-MM-dd HH:mm:ss" is a commonly used format for representing dates and times in many computer systems. The format string consists of various placeholders (such as yyyy for the year, MM for the month, dd for the day, HH for the hour, mm for the minute, and ss for the second) that are replaced with the actual values of the date and time being represented.

The DAYS constant is an array of strings that contains the names of the days, and the remaining constants are integers that correspond to each day of the week. This class provides a convenient way to refer to days of the week in your code, using the constants instead of hard-coded values or strings.

Below is the explanation of Each Method in StringUtil.java class

This StringUtil class provides some utility methods for working with strings:

The isNullOrEmpty method checks if a string is null or empty.

The join method joins an array of strings into a single string, separated by a delimiter.

The toTitleCase method converts a string to title case (i.e., capitalizes the first letter of each word).

Each method is marked as public static, which means that they can be called from other classes without creating an instance of the StringUtil class. The class is also marked as final, which means that it cannot be subclassed. This is a common practice for utility classes, as they usually don't need to be subclassed.

Clone this wiki locally