Vishal Anand
Vishal Anand

Reputation: 591

date parsing in java

I have written following program to convert date string to sql date object to store in db2 but the ouput shown is 2013-01-02 instead of 2013-02-02. can anyone explain why??

String string = "02/02/2013";
Date date = null;

try {
    date = new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH).parse(string);
    java.sql.Date newDate=new java.sql.Date(date.getTime());
    System.out.println(newDate);

} catch (ParseException e) {                    
    e.printStackTrace();
}

Upvotes: 0

Views: 138

Answers (2)

PermGenError
PermGenError

Reputation: 46428

your format should be

"dd/MM/yyyy"

note that mm is for minutes whereas MM is for Month

Check the Doc

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213281

For month you should use MM instead of mm. mm is used for minutes in hour: -

date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)

Upvotes: 2

Related Questions