Java String to Date stringToDate(String dateString)

Here you can find the source of stringToDate(String dateString)

Description

convert a string to a date using the standard web service pattern Note that HH is 0-23

License

Apache License

Parameter

Parameter Description
dateString a parameter

Return

the string, or null if the date was null

Declaration

public static Date stringToDate(String dateString) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Internet2/*from   w w w  .j  ava2  s. c o  m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.lang.reflect.Array;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Collection;

import java.util.Date;

import java.util.Map;

public class Main {
    /**
     * Note, this is 
     * web service format string
     */
    private static final String WS_DATE_FORMAT = "yyyy/MM/dd HH:mm:ss.SSS";
    /**
     * Note, this is 
     * web service format string
     */
    private static final String WS_DATE_FORMAT2 = "yyyy/MM/dd_HH:mm:ss.SSS";

    /**
     * convert a string to a date using the standard web service pattern Note
     * that HH is 0-23
     * 
     * @param dateString
     * @return the string, or null if the date was null
     */
    public static Date stringToDate(String dateString) {
        if (isBlank(dateString)) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                WS_DATE_FORMAT);
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(
                    WS_DATE_FORMAT2);
            try {
                return simpleDateFormat2.parse(dateString);
            } catch (ParseException e2) {
                throw new RuntimeException("Cannot convert '" + dateString
                        + "' to a date based on format: " + WS_DATE_FORMAT,
                        e);
            }
        }
    }

    /**
     * See if the input is null or if string, if it is empty or blank (whitespace)
     * @param input
     * @return true if blank
     */
    public static boolean isBlank(Object input) {
        if (null == input) {
            return true;
        }
        return (input instanceof String && isBlank((String) input));
    }

    /**
     * <p>Checks if a String is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * isBlank(null)      = true
     * isBlank("")        = true
     * isBlank(" ")       = true
     * isBlank("bob")     = false
     * isBlank("  bob  ") = false
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is null, empty or whitespace
     * @since 2.0
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Null safe array length or map
     * 
     * @param arrayOrCollection
     * @return the length of the array (0 for null)
     */
    public static int length(Object arrayOrCollection) {
        if (arrayOrCollection == null) {
            return 0;
        }
        if (arrayOrCollection.getClass().isArray()) {
            return Array.getLength(arrayOrCollection);
        }
        if (arrayOrCollection instanceof Collection) {
            return ((Collection) arrayOrCollection).size();
        }
        if (arrayOrCollection instanceof Map) {
            return ((Map) arrayOrCollection).size();
        }
        // simple non array non collection object
        return 1;
    }
}

Related

  1. stringToDate(String dateString)
  2. stringToDate(String dateString)
  3. stringToDate(String dateString)
  4. stringToDate(String dateString)
  5. stringToDate(String dateString)
  6. stringToDate(String dateString)
  7. stringToDate(String dateString, String dataFormat)
  8. stringToDate(String dateString, String dateFormat)
  9. stringTodate(String dateString, String format)