Java SQL Date Parse parseSqlDate(final String value)

Here you can find the source of parseSqlDate(final String value)

Description

parse Sql Date

License

Open Source License

Declaration

static java.sql.Date parseSqlDate(final String value) 

Method Source Code

//package com.java2s;
/*/*from  ww w. j ava  2  s .com*/
 * Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
 *
 * 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.text.ParseException;

public class Main {
    static final String SQL_DATE_FORMAT = "yyyy-MM-dd";

    static java.sql.Date parseSqlDate(final String value) {
        try {
            // JDK format in Date.valueOf is compatible with DATE_FORMAT
            return java.sql.Date.valueOf(value);
        } catch (IllegalArgumentException e) {
            return throwRuntimeParseException(value, new ParseException(value, 0), SQL_DATE_FORMAT);
        }
    }

    private static <T> T throwRuntimeParseException(String value, Exception e, String... legalFormats) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < legalFormats.length; i++) {
            sb.append("'").append(legalFormats[i]).append("'");
            if (i < legalFormats.length - 2) {
                sb.append(", ");
            }
        }
        throw new RuntimeException(
                "Unable to parse date from value: '" + value + "' ! Valid format are: " + sb.toString() + ".", e);
    }
}

Related

  1. parseDateE(Date dt)
  2. parseDateString(String tmp, String format)
  3. parseDateYYMMddHHmm(java.util.Date date)
  4. parseDateYYMMddHHmmss1(Date date)
  5. parseDateyyyy_MM_DD(String ds)
  6. parseSqlDate(final String value)
  7. parseSqlDate(java.util.Date date)
  8. parseSqlDate(java.util.Date date)
  9. parseSQLDate(String dateStr, String pattern)