Java Hour Format formatDate(Date date, boolean includeTime)

Here you can find the source of formatDate(Date date, boolean includeTime)

Description

Method formatDate.

License

Open Source License

Parameter

Parameter Description
date a parameter
includeTime a parameter

Declaration

public static String formatDate(Date date, boolean includeTime) 

Method Source Code

//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**/*from w w  w .j av a2 s .  c  o  m*/
     * Method formatDate.
     * 
     * @param date
     * @param includeTime
     * @return
     */
    public static String formatDate(Date date, boolean includeTime) {
        if (date != null) {
            if (includeTime)
                return new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").format(date);
            else
                return new SimpleDateFormat("MM/dd/yyyy").format(date);
        } else
            return null;
    }

    public static boolean formatDate(Date date) {
        boolean isValid = false;
        String formatDate = null;
        try {
            if (date != null) {
                SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
                formatDate = df.format(date);
                isValid = isValidDate(formatDate);
            }
        } catch (Exception e) {
            isValid = false;
        }
        return isValid;
    }

    /**
     * Method isValidDate. Validates that the given date string contains a valid date part (MM/DD/YYYY).
     * The "parseDate" method allows for a lot of garbage (e.g. "13/32/2000" translates as "02/01/2001")
     * @param dateVal
     * @return boolean
     */
    public static boolean isValidDate(String dateString) {
        // Null conditions
        if (dateString == null)
            return false;

        dateString = dateString.trim();
        if (dateString.length() == 0)
            return false;

        String datePart = null;
        int firstSpacePos = dateString.indexOf(' ');
        if (firstSpacePos > -1) {
            datePart = dateString.substring(0, firstSpacePos);
        } else {
            datePart = dateString;
        }

        // Validate date part: M/D/YYYY or MM/DD/YYYY
        if (datePart.length() < 8)
            return false;
        if (datePart.length() > 10)
            return false;
        int firstSlashPos = datePart.indexOf('/');
        if (firstSlashPos == -1)
            return false;
        int secondSlashPos = datePart.indexOf('/', firstSlashPos + 1);
        if (secondSlashPos == -1)
            return false;
        int month = getIntValue(datePart.substring(0, firstSlashPos), -1);
        if (month < 1 || month > 12)
            return false;
        int day = getIntValue(datePart.substring(firstSlashPos + 1, secondSlashPos), -1);
        if (day < 1 || day > 31)
            return false;
        int year = getIntValue(datePart.substring(secondSlashPos + 1), -1);
        if (year < 1000 || year > 9999)
            return false;

        // Month - day cross-checks
        switch (month) {
        case 4:
        case 6:
        case 9:
        case 11:
            if (day > 30)
                return false;
            break;

        case 2:

            if (day > 29)
                return false;

            if (year % 4 != 0) {
                if (day > 28)
                    return false;
            }

            else if (year % 100 == 0 && year % 400 != 0) {
                if (day > 28)
                    return false;
            }

            break;
        }

        return true;
    }

    /**
     * Method getIntValue. Returns the integer value associated with the passed string. Note Leading and Trailing blanks
     * are removed from the string before converting the value to an integer.
     * 
     * @param str
     *            The String being converted to an integer.
     * @param value
     *            Return this value instead if data when non-numeric character or null object is passed.
     * @return int The converted integer result
     */
    public static final int getIntValue(String str, int value) {
        int retVal = value;
        try {
            // String has a value
            if (str != null) {

                // Remove leading and trailing spaces.
                str = str.trim();

                // Chop decimal point and later
                int decimalPos = str.indexOf('.');
                if (decimalPos > -1)
                    str = str.substring(0, decimalPos);

                // Convert String to integer value
                retVal = Integer.parseInt(str);
            }

        } catch (Exception ignore) {
        }

        return retVal;
    }
}

Related

  1. formatCurrentTime(final String format)
  2. formatDate(Calendar currentDate, String pattern)
  3. formatDate(Calendar time)
  4. formatDate(Date d, String pattern, TimeZone tz)
  5. formatDate(Date d, TimeZone tz)
  6. formatDate(Date date, boolean time, boolean csv)
  7. formatDate(Date date, boolean withTime)
  8. formatDate(Date date, String dateFormat)
  9. formatDate(Date date, String expression)