Java Parse Date parseDate(@Nullable String value)

Here you can find the source of parseDate(@Nullable String value)

Description

parse Date

License

Open Source License

Declaration

@CheckForNull
    public static Date parseDate(@Nullable String value) 

Method Source Code


//package com.java2s;
/*//from  ww w . j  a  va2  s  .c o  m
 * SonarQube PDF Report
 * Copyright (C) 2010-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

public class Main {
    @CheckForNull
    public static Date parseDate(@Nullable String value) {
        return parseDate(value, "yyyy-MM-dd");
    }

    @CheckForNull
    private static Date parseDate(@Nullable String value, String format) {
        if (value != null) {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat(format);
                return dateFormat.parse(value);

            } catch (ParseException e) {
                throw new IllegalArgumentException("Fail to parse date '" + value + "': " + format, e);
            }
        }
        return null;
    }
}

Related

  1. parseDate(@Nonnull final String sFormat, @Nonnull final String sValue)
  2. parseDate(@Nonnull String stringDate)
  3. parseDate(Date date)
  4. parseDate(Date date)
  5. parseDate(Date date)
  6. parseDate(Date date)