Here you can find the source of parseDate(final String str)
Parses a string representing a date by trying different date patterns.
The following date patterns are tried (in the given order):
"yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd", "yyyy", "dd.MM.yyyy", "dd MMM yyyy", "dd MMM.License
Apache LicenseParameter
Parameter | Description |
---|---|
str | the date to parse, not null. |
Parameter | Description |
---|---|
ParseException | if no pattern matches. |
NullPointerException | if str is null. |
"today"
or "now"
.
public static Date parseDate(final String str) throws ParseException
//package com.java2s; /*//from w w w .j a va 2 s . c o m * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Main { private static final SimpleDateFormat DATE_PARSER = new SimpleDateFormat( "", Locale.ENGLISH); private static final ParsePosition DATE_PARSE_POSITION = new ParsePosition( 0); private static final String[] DATE_PATTERNS = new String[] { "yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd", "yyyy", "dd.MM.yyyy", "dd MMM yyyy", "dd MMM. yyyy", "MMMM yyyy", "MMM. dd, yyyy", "MMM. yyyy", "MMMM dd, yyyy", "MMM d, ''yy", "MMM. ''yy", "MMMM ''yy" }; /** * <p>Parses a string representing a date by trying different date patterns.</p> * * <p>The following date patterns are tried (in the given order):</p> * * <pre>"yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd", "yyyy", "dd.MM.yyyy", "dd MMM yyyy", * "dd MMM. yyyy", "MMMM yyyy", "MMM. dd, yyyy", "MMM. yyyy", "MMMM dd, yyyy", * "MMM d, ''yy", "MMM. ''yy", "MMMM ''yy"</pre> * * <p>A parse is only sucessful if it parses the whole of the input string. * If no parse patterns match, a ParseException is thrown.</p> * * <p>As a special case, the strings <code>"today"</code> and <code>"now"</code> * (ignoring case) return the current date.</p> * * @param str the date to parse, not null. * * @return the parsed date, or the current date if the input String (ignoring case) was * <code>"today"</code> or <code>"now"</code>. * * @throws ParseException if no pattern matches. * @throws NullPointerException if str is null. * * @since 1.1.1. */ public static Date parseDate(final String str) throws ParseException { if ("today".equalsIgnoreCase(str) || "now".equalsIgnoreCase(str)) { return new Date(); } for (int i = 0; i < DATE_PATTERNS.length; i++) { DATE_PARSER.applyPattern(DATE_PATTERNS[i]); DATE_PARSE_POSITION.setIndex(0); final Date date = DATE_PARSER.parse(str, DATE_PARSE_POSITION); if (date != null && DATE_PARSE_POSITION.getIndex() == str.length()) { return date; } } throw new ParseException("Unable to parse date: " + str, -1); } }