判断多种日期格式是否正确

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;


	public static boolean isDate(String date,String[] parsePatterns){
		boolean res = true;
		try {
			DateUtils.parseDateStrictly(date, parsePatterns);
		} catch (ParseException e) {
			res = false;
		}
		return res;
	}
	public static Date getDate(String date,String[] parsePatterns){
		try {
			return DateUtils.parseDateStrictly(date, parsePatterns);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	


public static void main(String[] args) {

		String str = "2013/1/33";
		String[] parsePatterns = { "yyyy-M-d", "yyyy/M/d" };
		
		boolean res=isDate(str,parsePatterns);
		System.out.println(isDate(str,parsePatterns));
		if(res){
			System.out.format("%1$tF\n", getDate(str,parsePatterns));
		}
	}

你可能感兴趣的:(日期格式)