469,323 Members | 1,667 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Check the Date Format.

dmjpro
2,476 2GB
Now a days I take care of sensitive coding rather than completing the code anyhow.
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
  1. Date checkDate(String date)throws Exception {
  2.         Date temp_d;
  3.         try{
  4.             temp_d = d.parse(date);
  5.             StringTokenizer st = new StringTokenizer(date,"-");
  6.             int m = Integer.parseInt(st.nextToken())-1,
  7.                 d = Integer.parseInt(st.nextToken()),
  8.                 y = Integer.parseInt(st.nextToken());
  9.             c.setTime(temp_d);
  10.             if(m!=c.get(Calendar.MONTH) || d!=c.get(Calendar.DATE) || y!=c.get(Calendar.YEAR)) throw new Exception("Wrong date format");
  11.             return temp_d;
  12.         }catch(Exception e)
  13.         {
  14.             throw e;
  15.         }
  16.     }
  17.  
Please do some comments on it.
Is anything there to improve the logic :-)

Kind regards,
Dmjpro.
Oct 4 '07 #1
4 12925
Ozzi
4
I would rather use SimpleDateFormat. Here is a very simple example of use.

Expand|Select|Wrap|Line Numbers
  1.  //example of String to test
  2. String myDate = "02.03.2007";
  3.  
  4. Date parsed = null;
  5. boolean formatOK = false;
  6.  
  7. //first correct format
  8. SimpleDateFormat format1 =
  9. new SimpleDateFormat("dd/MM/yyyy");
  10.  
  11. //second correct format
  12. SimpleDateFormat format2 =
  13. new SimpleDateFormat("dd.MM.yyyy");
  14.  
  15. //if an unarseable exception occurs, we have to try the second format
  16. try {
  17. parsed = format1.parse(myDate);
  18. formatOK = true;
  19. }catch (Exception e) {
  20. //do whatever
  21. }
  22.  
  23. //if the String doesn't follows the second format
  24. if(!formatOK){
  25. try {
  26. parsed = format2.parse(myDate);
  27. formatOK = true;
  28. } catch (ParseException e1) {
  29. //do whatever
  30. }
  31.  
  32. }
  33.  
  34. System.out.println(formatOK?"format OK":"format KO");
Oct 4 '07 #2
dmjpro
2,476 2GB
Ooops!
Sorry for some code missing :-)
The appropriate code is ........... "c" and "d" defined as

Expand|Select|Wrap|Line Numbers
  1. DateFormat d = new SimpleDateFormat("MM-dd-yyyy");
  2. Calendar c = new GregorianCalendar();
  3.  
Sorry ... Now it's Ok !
Please do some comments on it.

Kind regards,
Dmjpro.
Oct 4 '07 #3
dmjpro
2,476 2GB
I would rather use SimpleDateFormat. Here is a very simple example of use.

//example of String to test
String myDate = "02.03.2007";

Date parsed = null;
boolean formatOK = false;

//first correct format
SimpleDateFormat format1 =
new SimpleDateFormat("dd/MM/yyyy");

//second correct format
SimpleDateFormat format2 =
new SimpleDateFormat("dd.MM.yyyy");

//if an unarseable exception occurs, we have to try the second format
try {
parsed = format1.parse(myDate);
formatOK = true;
}catch (Exception e) {
//do whatever
}

//if the String doesn't follows the second format
if(!formatOK){
try {
parsed = format2.parse(myDate);
formatOK = true;
} catch (ParseException e1) {
//do whatever
}

}

System.out.println(formatOK?"format OK":"format KO");
I appreciate your coding :-)
But please, try to use Code tags.
Please read posting guidelines before Post.

Debasis Jana
Oct 4 '07 #4
dmjpro
2,476 2GB
I appreciate your coding :-)
But please, try to use Code tags.
Please read posting guidelines before Post.

Debasis Jana
Hey please do comments :-)

Debasis Jana
Oct 5 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Detlef Jockheck | last post: by
15 posts views Thread by Dan S | last post: by
1 post views Thread by Ricardo Silveira | last post: by
5 posts views Thread by TN Bella | last post: by
1 post views Thread by Thomas | last post: by
6 posts views Thread by Mike Charney | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.