Java Date ISO Parse isValidISO8601(String time)

Here you can find the source of isValidISO8601(String time)

Description

is Valid ISO

License

Open Source License

Declaration

public static boolean isValidISO8601(String time) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2013 Jesper ?qvist <jesper@llbit.se>
 *
 * This file is part of Chunky.// ww w .j av  a  2s  . c  o m
 *
 * Chunky is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Chunky 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 General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Chunky.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    public static boolean isValidISO8601(String time) {
        if (time.length() < 6) {
            return false;
        }
        try {
            // insert "GMT"
            if (time.endsWith("Z")) {
                time = time.substring(0, time.length() - 1) + "GMT-00:00";
            } else {
                time = time.substring(0, time.length() - 6) + "GMT"
                        + time.substring(time.length() - 6, time.length());
            }
            // http://stackoverflow.com/a/10624878/1250278
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
            dateFormat.parse(time);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }
}

Related

  1. iso8601ToDate(String s)
  2. ISO8601ToSeconds(String iso8601)
  3. isoDateStringToDate(String dateString)
  4. isOneOf(String value, String... values)
  5. ISOToJulianDate(String dateObs)
  6. normalizeToISO8601(String sDate, TimeZone tz)
  7. parseAwsFlavouredISO8601Date(String dateString)
  8. parseDate(String iso8061StrDateTime)
  9. parseDateISO(String date)