Java Parse Date Pattern YYYY parse(final String dateString)

Here you can find the source of parse(final String dateString)

Description

Parse the provided date.

License

Apache License

Parameter

Parameter Description
dateString The date string to parse

Return

parsed date

Declaration

public static Date parse(final String dateString) 

Method Source Code


//package com.java2s;
/*/*from w ww  .j a  v a  2  s .  c  om*/
 * Copyright 2017-2018 Crown Copyright
 *
 * 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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern TIMESTAMP_MILLISECONDS = Pattern.compile("^\\d*$");
    private static final Map<Pattern, String> FORMATS = new LinkedHashMap<>();
    private static final Pattern CHARS_TO_STRIP = Pattern.compile("[/_.:\\-| ]");
    private static final String ERROR_MSG = "The provided date string %s could not be parsed. "
            + "Please use a timestamp in milliseconds or one of the following formats: "
            + "[yyyy/MM, yyyy/MM/dd, yyyy/MM/dd HH, yyyy/MM/dd HH:mm, yyyy/MM/dd HH:mm:ss, yyyy/MM/dd HH:mm:ss.SSS]"
            + ". You can use a space, '-', '/', '_', ':', '|', or '.' to separate the parts.";

    /**
     * <p>
     * Parse the provided date.
     * </p>
     * The date string can be in any of the following formats:
     * <ul>
     * <li>timestamp in milliseconds</li>
     * <li>yyyy/MM</li>
     * <li>yyyy/MM/dd</li>
     * <li>yyyy/MM/dd HH</li>
     * <li>yyyy/MM/dd HH:mm</li>
     * <li>yyyy/MM/dd HH:mm:ss</li>
     * <li>yyyy/MM/dd HH:mm:ss.SSS</li>
     * </ul>
     * You can use a space, '-', '/', '_', ':', '|', or '.' to separate the parts.
     *
     * @param dateString The date string to parse
     * @return parsed date
     */
    public static Date parse(final String dateString) {
        return parse(dateString, null);
    }

    public static Date parse(final String dateString, final TimeZone timeZone) {
        if (null == dateString) {
            return null;
        }

        if (TIMESTAMP_MILLISECONDS.matcher(dateString).matches()) {
            try {
                return new Date(Long.parseLong(dateString));
            } catch (final NumberFormatException e) {
                throw new IllegalArgumentException(String.format(ERROR_MSG, dateString), e);
            }
        }

        final String formatedDateString = CHARS_TO_STRIP.matcher(dateString).replaceAll("");

        for (final Map.Entry<Pattern, String> entry : FORMATS.entrySet()) {
            if (entry.getKey().matcher(formatedDateString).matches()) {
                try {
                    final SimpleDateFormat sdf = new SimpleDateFormat(entry.getValue());
                    if (null != timeZone) {
                        sdf.setTimeZone(timeZone);
                    }
                    return sdf.parse(formatedDateString);
                } catch (final ParseException e) {
                    throw new IllegalArgumentException(String.format(ERROR_MSG, dateString), e);
                }
            }
        }
        throw new IllegalArgumentException(String.format(ERROR_MSG, dateString));
    }
}

Related

  1. parse(Class clazz, Object object)
  2. parse(Date d)
  3. parse(Date strDate, String pattern)
  4. parse(final String inputDate)
  5. parse(final String s)
  6. parse(final String source)
  7. parse(final String source)