Java Parse Date Pattern YYYY parse(final String inputDate)

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

Description

Parse Utc date string to date

License

Apache License

Parameter

Parameter Description
inputDate Utc date string

Return

date

Declaration

public static Date parse(final String inputDate) 

Method Source Code

//package com.java2s;
/**/*from ww w. j av a2s  . co  m*/
 * vertigo - simple java starter
 *
 * Copyright (C) 2013-2016, KleeGroup, direction.technique@kleegroup.com (http://www.kleegroup.com)
 * KleeGroup, Centre d'affaire la Boursidiere - BP 159 - 92357 Le Plessis Robinson Cedex - France
 *
 * 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.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import com.google.gson.JsonParseException;

public class Main {
    private static final String[] INPUT_DATE_FORMATS = new String[] {
            "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", //ISO8601
            "EEE, dd MMM yyyy HH:mm:ss zzz", // RFC 822, updated by RFC 1123
            "EEEE, dd-MMM-yy HH:mm:ss zzz", // RFC 850, obsoleted by RFC 1036
    //not supported : "EEE MMM d HH:mm:ss yyyy", // ANSI C's asctime() format
    };

    /**
     * Parse Utc date string to date
     * @param inputDate Utc date string
     * @return date
     */
    public static Date parse(final String inputDate) {
        final boolean isTruncatedDate = isTruncatedDate(inputDate);
        for (final String format : INPUT_DATE_FORMATS) {
            try {
                return createDateFormat(format, isTruncatedDate).parse(
                        inputDate);
            } catch (final ParseException e) {
                //nothing
            }
        }
        throw new JsonParseException("Unsupported Date format " + inputDate);
    }

    private static boolean isTruncatedDate(final Date date) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY) == 0
                && calendar.get(Calendar.MINUTE) == 0
                && calendar.get(Calendar.SECOND) == 0
                && calendar.get(Calendar.MILLISECOND) == 0;
    }

    private static boolean isTruncatedDate(final String dateStr) {
        return dateStr.endsWith("T00:00:00.000Z");
    }

    private static DateFormat createDateFormat(final String dateFormat,
            final boolean isTruncatedDate) {
        final DateFormat simpleDateFormat = new SimpleDateFormat(
                dateFormat, Locale.US);
        if (!isTruncatedDate) {
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        }
        return simpleDateFormat;
    }
}

Related

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