Description
Parse the given date string to date object and return a date instance based on the given date string.
License
Open Source License
Parameter
Parameter | Description |
---|
dateString | The date string to be parsed to date object. |
Exception
Parameter | Description |
---|
ParseException | If the date format pattern of the given date string isunknown, or if the given date string or its actual date isinvalid based on the date format pattern. |
Return
The parsed date object.
Declaration
public static Date parse(String dateString) throws ParseException
Method Source Code
//package com.java2s;
/*/* www . j av a 2 s. c o m*/
* net/balusc/util/DateUtil.java
*
* Copyright (C) 2007 BalusC
*
* This program 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 2 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if
* not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Main {
private static final Map<String, String> DATE_FORMAT_REGEXPS = new HashMap<String, String>() {
{
put("^\\d{8}$", "yyyyMMdd");
put("^\\d{1,2}\\.\\d{1,2}\\.\\d{4}$", "dd.MM.yyyy");
put("^\\d{1,2}-\\d{1,2}-\\d{4}$", "dd-MM-yyyy");
put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "dd/MM/yyyy");
put("^\\d{1,2}\\.\\d{1,2}\\.\\d{2}$", "dd.MM.yy");
put("^\\d{1,2}-\\d{1,2}-\\d{2}$", "dd-MM-yy");
put("^\\d{1,2}/\\d{1,2}/\\d{2}$", "dd/MM/yy");
put("^\\d{1,2}\\.\\d{4}$", "MM.yyyy");
put("^\\d{1,2}-\\d{4}$", "MM-yyyy");
put("^\\d{1,2}/\\d{4}$", "MM/yyyy");
put("^\\d{1,2}\\.\\d{2}$", "MM.yy");
put("^\\d{1,2}-\\d{2}$", "MM-yy");
put("^\\d{1,2}/\\d{2}$", "MM/yy");
put("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd");
put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "MM/dd/yyyy");
put("^\\d{4}/\\d{1,2}/\\d{1,2}$", "yyyy/MM/dd");
put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}$", "dd MMM yyyy");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}$", "dd MMMM yyyy");
put("^[a-z]{4,}\\s\\d{1,2}\\s\\d{4}$", "MMMM dd yyyy");
put("^\\d{1,2}-[a-z]{4,}-\\d{4}$", "dd-MMMM-yyyy");
put("^\\d{1,2}\\.[a-z]{4,}\\.\\d{4}$", "dd.MMMM.yyyy");
put("^\\d{1,2}\\s[a-z]{4,}$", "dd MMMM");
put("^[a-z]{4,}\\s\\d{1,2}$", "MMMM dd");
put("^\\d{1,2}\\s[a-z]{2,}$", "dd MMMM");
put("^\\d{1,2}-[a-z]{2,}$", "dd-MMMM");
put("^\\d{1,2}\\s[a-z]{2,}\\s\\d{4}$", "dd MMMM yyyy");
put("^\\d{1,2}/[a-z]{2,}/\\d{4}$", "dd/MMMM/yyyy");
put("^\\d{1,2}-[a-z]{2,}-\\d{4}$", "dd-MMMM-yyyy");
put("^\\d{1,2}\\.[a-z]{2,}\\.\\d{4}$", "dd.MMMM.yyyy");
put("^\\d{1,2}\\s[a-z]{2,}\\s\\d{2}$", "dd MMMM yy");
put("^\\d{1,2}/[a-z]{2,}/\\d{2}$", "dd/MMMM/yy");
put("^\\d{1,2}-[a-z]{2,}-\\d{2}$", "dd-MMMM-yy");
put("^\\d{1,2}\\.[a-z]{2,}\\.\\d{2}$", "dd.MMMM.yy");
put("^\\d{12}$", "yyyyMMddHHmm");
put("^\\d{8}\\s\\d{4}$", "yyyyMMdd HHmm");
put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}$", "dd-MM-yyyy HH:mm");
put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy-MM-dd HH:mm");
put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}$", "MM/dd/yyyy HH:mm");
put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy/MM/dd HH:mm");
put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMM yyyy HH:mm");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMMM yyyy HH:mm");
put("^\\d{14}$", "yyyyMMddHHmmss");
put("^\\d{8}\\s\\d{6}$", "yyyyMMdd HHmmss");
put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd-MM-yyyy HH:mm:ss");
put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy-MM-dd HH:mm:ss");
put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "MM/dd/yyyy HH:mm:ss");
put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy/MM/dd HH:mm:ss");
put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMM yyyy HH:mm:ss");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMMM yyyy HH:mm:ss");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}\\.\\d{6}$",
"dd MMMM yyyy HH:mm:ss.SSSSSS");
put("^\\d{1,2}\\s\\d{2}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}\\.\\d{6}$", "dd MM yyyy HH:mm:ss.SSSSSS");
put("^\\d{4}\\s\\d{2}\\s\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}\\.\\d{6}$", "yyyy MM dd HH:mm:ss.SSSSSS");
put("^\\d{4}-\\d{2}-\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}\\.\\d{6}$", "yyyy-MM-dd HH:mm:ss.SSSSSS");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}\\.\\d{2}$", "dd MMMM yyyy HH:mm:ss.SS");
put("^\\d{4}-\\d{2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}\\+\\d{2}:\\d{2}$", "yyyy-MM-dd'T'HH:mm:ssZZZ");
put("^\\d{4}$", "yyyy");
// put("^\\d{2}$", "yy");
}
};
/**
* Parse the given date string to date object and return a date instance
* based on the given date string. This makes use of the
* {@link DateUtil#determineDateFormat(String)} to determine the
* SimpleDateFormat pattern to be used for parsing.
*
* @param dateString
* The date string to be parsed to date object.
* @return The parsed date object.
* @throws ParseException
* If the date format pattern of the given date string is
* unknown, or if the given date string or its actual date is
* invalid based on the date format pattern.
*/
public static Date parse(String dateString) throws ParseException {
String dateFormat = determineDateFormat(dateString);
if (dateFormat == null) {
throw new ParseException("Unknown date format.", 0);
}
if (dateFormat.equals("MM/dd/yyyy")) {
Date d = null;
try {
d = parse(dateString, dateFormat);
} catch (Exception e) {
d = parse(dateString, "dd/MM/yyyy");
}
return d;
}
return parse(dateString, dateFormat);
}
/**
* Validate the actual date of the given date string based on the given date
* format pattern and return a date instance based on the given date string.
*
* @param dateString
* The date string.
* @param dateFormat
* The date format pattern which should respect the
* SimpleDateFormat rules.
* @return The parsed date object.
* @throws ParseException
* If the given date string or its actual date is invalid based
* on the given date format pattern.
* @see SimpleDateFormat
*/
public static Date parse(String dateString, String dateFormat) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
simpleDateFormat.setLenient(false); // Don't automatically convert
// invalid date.
return simpleDateFormat.parse(dateString);
}
/**
* Determine SimpleDateFormat pattern matching with the given date string.
* Returns null if format is unknown. You can simply extend DateUtil with
* more formats if needed.
*
* @param dateString
* The date string to determine the SimpleDateFormat pattern for.
* @return The matching SimpleDateFormat pattern, or null if format is
* unknown.
* @see SimpleDateFormat
*/
public static String determineDateFormat(String dateString) {
for (String regexp : DATE_FORMAT_REGEXPS.keySet()) {
if (dateString.toLowerCase().matches(regexp)) {
return DATE_FORMAT_REGEXPS.get(regexp);
}
if (dateString.matches(regexp)) {
return DATE_FORMAT_REGEXPS.get(regexp);
}
}
return null; // Unknown format.
}
}
Related
- parse(String DateString)
- parse(String dateString)
- parse(String dateString)
- parse(String datestring)
- parse(String dateString)
- parse(String dateString)
- parse(String dateString, String dateFormat)
- parse(String dateText)
- parse(String ds)