Java LocalDate from String toLocalDate(String dateValue)

Here you can find the source of toLocalDate(String dateValue)

Description

This method parse date from String object to LocalDate object.

License

Apache License

Parameter

Parameter Description
dateValue Data value in format "MM/dd/yyyy".

Return

Parsed date to LocalDate object.

Declaration

public static LocalDate toLocalDate(String dateValue) 

Method Source Code


//package com.java2s;
/*/* w  w w. ja  v  a2  s. co m*/
 * Copyright 2015 hilemz
 *
 * http://www.wykop.pl/ludzie/hilemz/
 *
 * 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.time.LocalDate;

import java.time.format.DateTimeFormatter;

public class Main {
    /**
     * This method parse date from String object to LocalDate object.
     *
     * @param dateValue
     *            Data value in format "MM/dd/yyyy".
     * @return Parsed date to LocalDate object.
     */
    public static LocalDate toLocalDate(String dateValue) {
        return LocalDate.parse(trimQuotes(dateValue), DateTimeFormatter.ofPattern("M/dd/yyyy"));
    }

    /**
     * This method trim " from passed value.
     * 
     * @param value
     *            Value to trim.
     * @return Trimmed value.
     */
    public static String trimQuotes(String value) {
        if (!isParsable(value)) {
            throw new IllegalArgumentException("Illegal argument value.");
        }
        return value.replaceAll("^\"|\"$", "");
    }

    /**
     * This method verificate if passed value is appropriate for parsing.
     *
     * @param value
     *            Value to check.
     * @return Result of verification.
     */
    public static boolean isParsable(String value) {
        return !(value == null || value.equals("N/A") || value.equals("") || value.equals("\"\""));
    }
}

Related

  1. parseLocalDate(String inPattern, String inDate)
  2. parseLocalDate(String localDate)
  3. parseStringToLocalDate(String strDate)
  4. toLocalDate(String date)
  5. toLocalDate(String date)