File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes using parse() from DateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using parse() from DateFormat" Watch "using parse() from DateFormat" New topic

using parse() from DateFormat

Vishnu Munnangi
Ranch Hand

Joined: Sep 28, 2004
Posts: 114
Hi Everyone,

I have a basic question reg validating a date.

Pl go thru the sample program

public static void main(String[] args) {
Date dtParsedDate = null;
DateFormat dtFormattedDate = null;
dtFormattedDate = new SimpleDateFormat("mm/dd/yyyy");//1
dtParsedDate = dtFormattedDate.parse("20/05/2005");//2
System.out.println("Correct Date");
}catch(ParseException ex){
System.out.println("Incorrect Format" + ex.getMessage());

here when i am running the program, it gives me the output as "Correct Date"
at step 1, when i give the format as
dtFormattedDate = new SimpleDateFormat("MM/DD/YYYY");
then even if i enter the correct date for ex: 02/12/2005, even then it gives me the output saying Incorrect Format.
I am not able to understand what does parse() doing.
Also at step3, dtParsedDate displays wrong date. It doesn't print the input that i give

Can any one pl explain me about the parse() and the difference between the formats in "mm/dd/yyyy" and "MM/DD/YYYY" in step 1.

Thanks very much for your time
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Please, look the API for class SimpleDateFormat.

Letter Date or Time Presentation Examples
y Year Year 1996; 96
M Month in year Month July; Jul; 07
m Minute in hour Number 30
D Day in year Number 189
d Day in month Number 10

[My Blog]
All roads lead to JavaRanch
Vishnu Munnangi
Ranch Hand

Joined: Sep 28, 2004
Posts: 114
thanks for the reply.
I agree. Here's the link:
subject: using parse() from DateFormat
It's not a secret anymore!