Java Date Value Check isDate(final String date)

Here you can find the source of isDate(final String date)

Description

Returns whether or not the string can be parsed as a date.

License

Open Source License

Parameter

Parameter Description
date a parameter

Declaration

public static boolean isDate(final String date) 

Method Source Code

//package com.java2s;
/*/* w w  w .j  a va 2s.c  o m*/
 * Copyright (c) 2015 Christopher Ritchie
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

import java.text.DateFormat;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Returns whether or not the string can be parsed as a date.
     * @param date
     * @return
     */
    public static boolean isDate(final String date) {
        DateFormat format = DateFormat.getDateInstance();
        try {
            format.parse(date);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * Splits the string based on the given separator.
     * @param string
     * @param separator
     * @return
     * @throws Exception
     */
    public static List<String> parse(final String string, final String separator, boolean trim) throws Exception {
        String left = string;
        List<String> tokens = new ArrayList<String>();
        while (left.indexOf(separator) >= 0) {
            int index = left.indexOf(separator);
            if (trim) {
                tokens.add(left.substring(0, index).trim());
            } else {
                tokens.add(left.substring(0, index));
            }
            left = left.substring(index + separator.length());
        }
        tokens.add(left.substring(0));
        return tokens;
    }

    /**
     * Splits the csv string.
     * @param string
     * @return
     * @throws Exception
     */
    public static List<String> parse(final String csvString, boolean trim) throws Exception {
        return parse(csvString, ",", trim);
    }

    /**
     * Splits the csv string.
     * @param string
     * @return
     * @throws Exception
     */
    public static List<String> parse(final String csvString) throws Exception {
        return parse(csvString, ",", true);
    }
}

Related

  1. isData(long date)
  2. isDataOra(String date)
  3. isDate(final String date)
  4. isDate(final String dateString)
  5. isDate(final String strDate, final String pattern)
  6. isDate(Object object)
  7. isDate(String date)