zoukankan      html  css  js  c++  java
  • Java校验8位字符串是否为正确的日期格式

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    
    /**
     * 校验8位字符串是否为正确的日期格式
     * @author 【J.H】
     * 参考:https://blog.csdn.net/cc_yy_zh/article/details/73181010
     */
    public class Demo1 {
    	//校验8位字符串是否为正确的日期格式
    	private static boolean isValidDate(String str) {
    	    boolean result = true;
    	    //判断字符串长度是否为8位
    	    if(str.length() == 8){
    	    	// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
    		    //SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    	    	SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    		    try {
    		        // 设置lenient为false.
    		        // 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
    		        format.setLenient(false);
    		        format.parse(str);
    		    } catch (ParseException e) {
    		        // e.printStackTrace();
    		        // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
    		    	result = false;
    		    }
    	    }else{
    	    	result = false;
    	    }
    	    
    	    return result;
    	}
    	
    	public static void main(String[] args) {
    		 String s = "2018042";
    		 System.out.println(isValidDate(s));
    	}
    }
    
  • 相关阅读:
    .c 文件取为.o文件
    wildcard 处理全部文件
    专家解读Linux操作系统内核中的GCC特性
    Yeoman:适合现代Web应用的现代工作流
    【转】nodejs
    node.js
    2019暑假集训 种树
    2019.6.5 NOIP2014 day2 t2 寻找道路
    2019.6.1 最优贸易
    2019.5.11 海淀区赛之杯子
  • 原文地址:https://www.cnblogs.com/Big-Boss/p/9431653.html
Copyright © 2011-2022 走看看