Sunday, January 8, 2017

DateUtility class for converting String into Date and Vice Versa using Java

In this post, we will see how to convert String into Date and vice versa.

DateUtility.java
package com.ranga.utility;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.TimeZone;

/*
*  Utility class for converting string into date.
*  @author Ranga Reddy
*  @version 1.0
*  @since 08/Jan/2017
*/
public class DateUtility {

    /**
     * Used to get the date in Date format.
     * @param dateInString - date in string format
     * @param format - format to get the date.
     * @return date parsed from the string.
     * @throws ParseException
     */
    public static Date parseDate(String dateInString, String format) throws ParseException {
        Objects.requireNonNull(dateInString, "Date string value must not be null.");
        Objects.requireNonNull(format, "Format must not be null.");
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(dateInString);
    }

    /**
     * Used to get the date in String format.
     * @param date - the date
     * @param pattern - the pattern
     * @return the date in String.
     */
    public static String format(Date date, String pattern) {
        return format(date, pattern, null);
    }

    /**
     * Used to get the date in String format.
     * @param date - the date
     * @param pattern - the pattern
     * @param timeZone - the timeZone
     * @return
     */
    public static String format(Date date, String pattern, TimeZone timeZone) {
        Objects.requireNonNull(date, "Date must not be null.");
        Objects.requireNonNull(pattern, "Pattern must not be null.");
        DateFormat formatter = new SimpleDateFormat(pattern);
        if (timeZone != null) {
            formatter.setTimeZone(timeZone);
        }
        return formatter.format(date);
    }

    /**
     * Used to convert the date in one format to another format.
     * @param fromDate - the from date
     * @param fromPattern - the from pattern
     * @param toPattern - the to pattern
     * @return formated date
     * @throws ParseException
     */
    public static String convertDateFromOneFormatToAnother(String fromDate, String fromPattern, String toPattern)
            throws ParseException {
        Objects.requireNonNull(fromDate, "From Date must not be null.");
        Objects.requireNonNull(fromPattern, "From Pattern must not be null.");
        Objects.requireNonNull(toPattern, "To Pattern must not be null.");
        
        DateFormat fromDateFormat = new SimpleDateFormat(fromPattern);
        Date sourceDate = fromDateFormat.parse(fromDate);
        DateFormat toDateFormat = new SimpleDateFormat(toPattern);
        return toDateFormat.format(sourceDate);
    }


    /**
     * Used to validate the date.
     * 
     * @param date - date in string.
     * @param format - format to check the date
     * @return true if date is valid otherwise return false.
     */
    public static boolean isValidDate(String date, String pattern) {
        Objects.requireNonNull(date, "Date must not be null.");
        Objects.requireNonNull(pattern, "Pattern must not be null.");

        DateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            dateFormat.parse(date);
            return true;
        } catch (Exception e) {
            System.err.println("Encountered exception while validating the date. " + e);
            return false;
        }
    }
}
DateUtilityTest.java
package com.ranga.utility;

import java.text.ParseException;
import java.util.Date;

/**
 * Used to test the DateUtility functionalities.
 * 
 * @author Ranga Reddy
 * @version 1.0
 * @since 08/Jan/2017
 */
public class DateUtilityTest {
    public static void main(String[] args) throws ParseException {
        
        Date date = new Date();
        String formatedDate = DateUtility.format(date, "yyyy-MMM-dd HH:mm:ss a");
        System.out.println("Formatted date: "+formatedDate);
        
        String parseDateString = "01-Jul-1988";
        Date convertedDate = DateUtility.parseDate(parseDateString, "dd-MMM-yyyy");
        System.out.println("Converted date: "+convertedDate);
        
        boolean isValidDate = DateUtility.isValidDate(formatedDate, "yyyy-MMM-dd");
        System.out.println("IsValid date: "+isValidDate);
    }
}
Output:
Formatted date: 2017-Jan-08 19:13:32 PM
Converted date: Fri Jul 01 00:00:00 IST 1988
IsValid date: true
Happy Learning ...!

0 comments: