Java Date ISO Parse strongCheckIso8601Date(String date)

Here you can find the source of strongCheckIso8601Date(String date)

Description

full check of whether the date is a valid iso 8601 date or not.

License

Open Source License

Declaration

public static boolean strongCheckIso8601Date(String date) 

Method Source Code

//package com.java2s;
/**//from   w  w  w.  j  ava 2  s.c o m
 *     This file is part of the Squashtest platform.
 *     Copyright (C) 2010 - 2016 Henix, henix.fr
 *
 *     See the NOTICE file distributed with this work for additional
 *     information regarding copyright ownership.
 *
 *     This 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 software 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 software.  If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.util.Date;

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

    /**
     * full check of whether the date is a valid iso 8601 date or not. Potentially slower than
     * #weakCheckIso8601Date but safer.
     *
     */
    public static boolean strongCheckIso8601Date(String date) {
        if (date == null) {
            return false;
        } else {
            try {
                parseIso8601Date(date);
                return true;
            } catch (ParseException e) {
                return false;
            }
        }
    }

    /**
     * @param strDate
     * @return the Date obtained when parsing the argument against pattern yyyy-MM-dd
     */
    public static Date parseIso8601Date(String strDate) throws ParseException {
        if (strDate == null) {
            return null;
        } else {
            return parseDate(strDate, ISO_DATE);
        }
    }

    private static Date parseDate(String strDatetime, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(strDatetime);
    }
}

Related

  1. parseIsoDateInput(String str)
  2. parseIsoDateTime(String isoDateTime)
  3. parseISODuration(String duration)
  4. parseIsoString(String time)
  5. parseTimestampIso8601(String timestamp)
  6. time2StringISO(Date date)
  7. timestampAsIsoString(long timestamp)
  8. toCalendar(final String iso8601string)
  9. toDate(String isoDate)