Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Testing for valid date?

Posted on 2007-08-08
7
Medium Priority
?
319 Views
Last Modified: 2013-11-05
Dear Experts,

This is an easy one (I hope).

Given a String, what is the elegant way to test if it contains a valid CCYYMMDD date?  I know I can parse it by hand, but is there a simpler way,  using Calendar, for instance?

Thanks,
BrianMc1958
0
Comment
Question by:BrianMc1958
  • 4
  • 3
7 Comments
 
LVL 2

Expert Comment

by:tbeernot
ID: 19654648
Use SimpleDateFormat
0
 

Author Comment

by:BrianMc1958
ID: 19654709
I'm trying it, but I'm not getting it to work with "20071231", for instance.
0
 
LVL 2

Accepted Solution

by:
tbeernot earned 500 total points
ID: 19654754
public static void main(String[] args)
{
      try
      {
            SimpleDateFormat lSimpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            System.out.println(lSimpleDateFormat.parse("20071231"));
      }
      catch (Throwable t)
      {
            t.printStackTrace();
      }
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:BrianMc1958
ID: 19654809
That compiles and runs.  However, it accepts "20071232".  (It decides that equals 20080101).  If nobody else responds, I'll award points anyway, but I'd like to wait a little while longer.

Would you have another suggestion that would validate the month and day?
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 19655062
This is the behavior of the Java parsers. Otherwise you have to do it yourself.
0
 

Author Comment

by:BrianMc1958
ID: 19655459
To anyone reading this, add this line to the above:

                  lSimpleDateFormat.setLenient(false);

It works.

Thanks very much...
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 19656076
setLenient... Interesting. Thanks!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question