473,388 Members | 1,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Check the Date Format.

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.     }
Please do some comments on it.
Is anything there to improve the logic :-)

Kind regards,
Oct 4 '07 #1
4 13201
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";
  4. Date parsed = null;
  5. boolean formatOK = false;
  7. //first correct format
  8. SimpleDateFormat format1 =
  9. new SimpleDateFormat("dd/MM/yyyy");
  11. //second correct format
  12. SimpleDateFormat format2 =
  13. new SimpleDateFormat("dd.MM.yyyy");
  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. }
  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. }
  32. }
  34. System.out.println(formatOK?"format OK":"format KO");
Oct 4 '07 #2
2,476 2GB
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();
Sorry ... Now it's Ok !
Please do some comments on it.

Kind regards,
Oct 4 '07 #3
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
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
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

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

Similar topics

by: Detlef Jockheck | last post by:
Hi, I would like to check if a string contains a valid date format (Format: "dd.mm.yyyy") or not. What is the best way to do this. regexp? At the moment it would be sufficient to check for...
by: Dan S | last post by:
My application asks the user to enter in a date - in the mm/dd/yyyy format. Is there any quick and easy way to verify the date the user enters is formatted correctly? Right now I'm calling...
by: Ricardo Silveira | last post by:
I'm validating a text box using a Compare Validator Component with the folowing properties: Operator = DataTypeCheck Type= Date But the web application is validating an english format date...
by: TN Bella | last post by:
Trying to check if an entry for an date is < 3 years to current date or > 30 days from current date. How can I do this in asp.net....? Do I need to use the customvalidation. Can anyone give me an...
by: Thomas | last post by:
Hi all, i have to check if a value is a valid date format depending on a "format string" the user specified. For example: Format String: "dd/mm/yyyy" Date Value: "01/12/2006" Valid: Yes! ...
by: Mike Charney | last post by:
Is there a way to check a files date and time stamp from VBA in access. I have a need check a date stamp on a file that I am importing. Thanks in advance, Mike m charney at dunlap hospital...
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
by: Lester | last post by:
I'm driving myself crazy with a problem in trying to translate a query written for Access to that for SQL server. I would think that I would use a trigger, but am not sure how to set it up. We...
by: ollyb303 | last post by:
Hello, I am using a dynamic crosstab report to track performance statistics for my company and I have hit a problem. I would like the option to track stats daily (for the last 7 complete...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.