Java Parse Date parseDate(String sDate)

Here you can find the source of parseDate(String sDate)

Description

Parse the given date ( supposed to be in ISO format : "YYYY-MM-DD" )

License

LGPL

Parameter

Parameter Description
sDate a parameter

Exception

Parameter Description
TelosysRuntimeException if the date is invalid

Return

date or null if the given string is null or void

Declaration

public static java.util.Date parseDate(String sDate) 

Method Source Code

//package com.java2s;
/**//w  ww .j  a  v a 2 s.  c om
 *  Copyright (C) 2008-2014  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.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.gnu.org/licenses/lgpl.html
 *
 *  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.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {
    private final static String DATE_ISO_FORMAT = "yyyy-MM-dd";
    private final static String INVALID_DATE_FORMAT = "invalid format 'YYYY-MM-DD' expected";

    /**
     * Parse the given date ( supposed to be in ISO format : "YYYY-MM-DD" )
     * @param sDate
     * @return date or null if the given string is null or void
     * @throws TelosysRuntimeException if the date is invalid
     * @since 1.0.2
     */
    public static java.util.Date parseDate(String sDate) {
        if (sDate == null)
            return null;
        if (sDate.length() == 0)
            return null;

        char c = 0;
        for (int i = 0; i < 10; i++) // the length is 10 "YYYY-MM-DD"
        {
            c = sDate.charAt(i);
            if ((c < '0' || c > '9') && (c != '-')) {
                throwParseDateException(sDate, INVALID_DATE_FORMAT);
            }
            if (c == '-' && (i != 4 && i != 7)) {
                throwParseDateException(sDate, INVALID_DATE_FORMAT);
            }
        }

        java.util.Date ret = null;
        try {
            //--- Try to parse the input date ( with non lenient parsing => check validity )
            //            DATE_FORMAT_ISO.setLenient(false); // non lenient parsing ( exception if invalid date )
            //            ret = DATE_FORMAT_ISO.parse(sDate);

            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_ISO_FORMAT);
            dateFormat.setLenient(false); // non lenient parsing ( exception if invalid date )
            ret = dateFormat.parse(sDate);

        } catch (ParseException e) {
            throwParseDateException(sDate, "invalid date");
        }
        return ret;
    }

    private static void throwParseDateException(String sDate, String sMsg) {
        throw new RuntimeException("Cannot parse date '" + sDate + "' : " + sMsg);
    }
}

Related

  1. parseDate(String s)
  2. parseDate(String s)
  3. parseDate(String s)
  4. parsedate(String s, SimpleDateFormat fmt)
  5. parseDate(String s, String format)
  6. parseDate(String sDate)
  7. parseDate(String sDate)
  8. parseDate(String sDate, Locale locale)
  9. parseDate(String sDate, SimpleDateFormat _dateFormat)