So have a look at this code.
I am trying to check the Date Format.
I am here assuming that the date separator string is either "-" or "/".
First I thought to do it using Regular Expression, but I am new to Regular Expression.
So experts please do some comments here to improve the sensitivity of this code.
Expand|Select|Wrap|Line Numbers
- Date checkDate(String date)throws Exception {
- Date temp_d;
- try{
- temp_d = d.parse(date);
- StringTokenizer st = new StringTokenizer(date,"-");
- int m = Integer.parseInt(st.nextToken())-1,
- d = Integer.parseInt(st.nextToken()),
- y = Integer.parseInt(st.nextToken());
- c.setTime(temp_d);
- if(m!=c.get(Calendar.MONTH) || d!=c.get(Calendar.DATE) || y!=c.get(Calendar.YEAR)) throw new Exception("Wrong date format");
- return temp_d;
- }catch(Exception e)
- {
- throw e;
- }
- }
Is anything there to improve the logic :-)
Kind regards,
Dmjpro.