Java Parse Date parseDate(String date)

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

Description

Parses a string into a date.

License

Open Source License

Parameter

Parameter Description
date string that represents an ISO 8601 date and time

Exception

Parameter Description
IllegalArgumentException if the string could not be parsed as adate

Return

parsed date, or null if the argument is null

Declaration

public static Date parseDate(String date) 

Method Source Code

//package com.java2s;
/*//ww w  .  ja v  a2 s . c om
 * ClientUtilities
 * Copyright (C) 2015 Nishimura Software Studio
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero 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 Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    private static final DateFormat FINE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

    /**
     * Parses a string into a date.
     * @param date string that represents an ISO 8601 date and time
     * @return parsed date, or <code>null</code> if the argument is
     * <code>null</code>
     * @throws IllegalArgumentException if the string could not be parsed as a
     * date
     */
    public static Date parseDate(String date) {
        if (date == null) {
            return null;
        }

        try {
            int p = date.indexOf('.');
            if (p >= 0) {
                // Counts the number of digits after the decimal point.
                int q = p + 1;
                while (q < date.length() && Character.isDigit(date.charAt(q))) {
                    q += 1;
                }
                if (q >= p + 4) {
                    if (q > p + 4) {
                        // Cuts off sub-millisecond digits.
                        date = date.substring(0, p + 4) + date.substring(q);
                    }
                    return FINE_DATE_FORMAT.parse(date);
                }

                // Cuts off sub-second digits.
                date = date.substring(0, p) + date.substring(q);
            }
            return DATE_FORMAT.parse(date);
        } catch (ParseException exception) {
            throw new IllegalArgumentException("\"" + date + "\" could not be parsed as Date", exception);
        }
    }
}

Related

  1. parseDate(String date)
  2. parseDate(String date)
  3. parseDate(String date)
  4. parseDate(String date)
  5. parseDate(String date)
  6. parseDate(String date)
  7. parseDate(String date)
  8. parseDate(String date)
  9. parseDate(String date)