`
penggle
  • 浏览: 57930 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

java判断字符串是否是日期

    博客分类:
  • j2se
阅读更多
public class StringUtil {
	
	/**
	 * 判断字符串值是否为空
	 * @param value
	 * @return
	 */
	public static boolean isEmpty(String value){
		if(value == null || "".equals(value)){
			return true;
		}
		return false;
	}
	
	public static boolean isDate(String value,String format){
		
		SimpleDateFormat sdf = null;
		ParsePosition pos = new ParsePosition(0);//指定从所传字符串的首位开始解析
		
		if(value == null || isEmpty(format)){
			return false;
		}
		try {
			sdf = new SimpleDateFormat(format);
			sdf.setLenient(false);
			Date date = sdf.parse(value,pos);
			if(date == null){
				return false;
			}else{
				System.out.println("-------->pos : " + pos.getIndex());
				System.out.println("-------->date : " + sdf.format(date));
				//更为严谨的日期,如2011-03-024认为是不合法的
				if(pos.getIndex() > sdf.format(date).length()){
					return false;
				}
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String[] args) {
		System.out.println(isDate("21011-02-18","yyyy-MM-dd"));
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics