使用正则表达式JAVA验证日期格式

时间:2017-05-22 11:30:49

标签: regex-lookarounds

Can we validate dd/MM/yyyy and MM/dd/yyyy format together using regex.

我想验证用户的任何有效日期格式,我为所有其他格式编写了正则表达式,但无法将dd / MM / yyyy和MM / dd / yyyy区分开来。

1 个答案:

答案 0 :(得分:0)

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 
 */

/**
 * @author Ravi Shankar
 *
 */
public class DateFormater {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new DateFormater().dateParser("CURR-MAY");
    }


    public String dateParser(String paramDate){

        String date=null;
        DateFormat df= null;
        DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

        // 
        if(null != paramDate && !paramDate.equalsIgnoreCase("")){
            paramDate = paramDate.trim();
            if(paramDate.matches("\\d{4}[/]\\d{1,2}[/]\\d{1,2}")){        
                df = new SimpleDateFormat("yyyy/MM/dd");
            }else if(paramDate.matches("\\d{4}[-]\\d{1,2}[-]\\d{1,2}")){  
                df = new SimpleDateFormat("yyyy-MM-dd");
            }else if(paramDate.matches("\\d{4}[.]\\d{1,2}[.]\\d{1,2}")){  
                df = new SimpleDateFormat("yyyy.MM.dd");
            }else if(paramDate.matches("\\d{1,2}[/]\\d{1,2}[/]\\d{2}")){  
                df = new SimpleDateFormat("MM/dd/yy");
            }else if(paramDate.matches("\\d{1,2}[-]\\d{1,2}[-]\\d{2}")){  
                df = new SimpleDateFormat("MM-dd-yy");
            }else if(paramDate.matches("\\d{1,2}[.]\\d{1,2}[.]\\d{2}")){  
                df = new SimpleDateFormat("MM.dd.yy");
            }else if(paramDate.matches("\\d{1,2}[/]\\d{1,2}[/]\\d{4}")){  
                df = new SimpleDateFormat("MM/dd/yyyy");
            }else if(paramDate.matches("\\d{1,2}[-]\\d{1,2}[-]\\d{4}")){  
                df = new SimpleDateFormat("MM-dd-yyyy");
            }else if(paramDate.matches("\\d{1,2}[.]\\d{1,2}[.]\\d{4}")){  
                df = new SimpleDateFormat("MM.dd.yyyy");
            }else if(paramDate.matches("\\d{1,2}[/]\\d{1,2}")){  
                df = new SimpleDateFormat("MM/dd/yyyy");
                paramDate =  addYear(paramDate);
            }else if(paramDate.matches("\\d{1,2}[-]\\d{1,2}")){  
                df = new SimpleDateFormat("MM-dd-yyyy");
                paramDate =  addYear(paramDate);
            }else if(paramDate.matches("\\d{1,2}[.]\\d{1,2}")){  
                df = new SimpleDateFormat("MM.dd.yyyy");
                paramDate =  addYear(paramDate);
            }else if(paramDate.matches("\\d{1,2}[/]\\d{4}")){  
                df = new SimpleDateFormat("MM/yyyy");
            }else if(paramDate.matches("\\d{1,2}[-]\\d{4}")){  
                df = new SimpleDateFormat("MM-yyyy");
            }else if(paramDate.matches("\\d{1,2}[.]\\d{4}")){  
                df = new SimpleDateFormat("MM.yyyy");
            }else if(paramDate.matches("\\S{3}[/]\\d{2}")){  
                df = new SimpleDateFormat("MMM/yy");
            }else if(paramDate.matches("\\S{3}[-]\\d{2}")){  
                df = new SimpleDateFormat("MM-yy");
            }else if(paramDate.matches("\\S{3}[.]\\d{2}")){  
                df = new SimpleDateFormat("MM.yy");
            }else if(paramDate.matches("\\d{1,2}[/]\\S{3}")){  
                df = new SimpleDateFormat("MM/dd/yyyy");
                paramDate =  addYear2(paramDate);
            }else if(paramDate.matches("\\d{1,2}[-]\\S{3}")){  
                df = new SimpleDateFormat("MM-dd-yyyy");
                paramDate =  addYear2(paramDate);
            }else if(paramDate.matches("\\d{1,2}[.]\\S{3}")){  
                df = new SimpleDateFormat("MM.dd.yyyy");
                paramDate =  addYear2(paramDate);
            }else if(paramDate.matches("\\S{3}")){  
                df = new SimpleDateFormat("MM/dd/yyyy");
                paramDate =  addYear3(paramDate);
            }else if(paramDate.matches("\\S{3,6}[-]\\S{3}")){  
                df = new SimpleDateFormat("MM/dd/yyyy");
                paramDate =  addYear3(paramDate);
            }

            try {
                date = sdf.format(df.parse(paramDate));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return date;
    }


    /**
     * @param paramDate
     * Adding Year in MM/DD
     * @return
     */
    public String addYear(String paramDate){
        int month = 0;
        String sp= "";
        if(paramDate.matches("\\d{1,2}[/]\\d{1,2}")){  
            month =Integer.parseInt(paramDate.split("/")[0]);
            sp="/";
        }else if(paramDate.matches("\\d{1,2}[-]\\d{1,2}")){  
            month =Integer.parseInt(paramDate.split("-")[0]);
            sp="-";
        }else if(paramDate.matches("\\d{1,2}[.]\\d{1,2}")){  
            month =Integer.parseInt(paramDate.split(".")[0]);
            sp=".";
        }

        month =Integer.parseInt(paramDate.split("/")[0]);
        Calendar cal = Calendar.getInstance();
        int cm = cal.get(Calendar.MONTH) + 1;
        int year = 0;
        if((month - cm)>=0){
            year = cal.get(Calendar.YEAR);
        }else{
            year = cal.get(Calendar.YEAR)+1;
        }
        return paramDate= paramDate+sp+year;
    }


    /**
     * @param paramDate
     * Converting dd/MMM to MM/dd and adding yyyy
     * @return
     */
    public String addYear2(String paramDate){
        String date="";
        DateFormat sdf = new SimpleDateFormat("MM/dd"); ;
        DateFormat df = new SimpleDateFormat("dd/MMM"); ;
        if(paramDate.matches("\\d{2}[-]\\S{3}")){  
            df = new SimpleDateFormat("dd-MMM");
        }else if(paramDate.matches("\\d{2}[.]\\S{3}")){  
            df = new SimpleDateFormat("dd.MMM");
        }
        try {
            date = addYear(sdf.format(df.parse(paramDate)));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

    /**
     * @param paramDate
     * converting  MMM to MM/dd and adding yyyy 
     * @return
     */
    public String addYear3(String paramDate){
        String date="";
        DateFormat sdf = new SimpleDateFormat("MM/dd");
        DateFormat df = new SimpleDateFormat("MMM"); 
        if(paramDate.matches("\\S{3,6}[-]\\S{3}")){
            paramDate=paramDate.split("-")[1];
        }
        try {
            date = addYear(sdf.format(df.parse(paramDate)));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

}