user2925107
user2925107

Reputation: 3

Invalid date value after parse

I am new to java. I have a Date that is stored in the variable, pubDate = "2013-09-23"

When I'm executing this

SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);

I'm getting wrong value : Wed Jan 23 00:09:00 GMT+05:30 2013

Please help me why it so. and help me to solve this.

Upvotes: 0

Views: 171

Answers (1)

Alexis C.
Alexis C.

Reputation: 93842

M is for Month in year while m is for Minute in hour

You should use SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

String pubDate = "2013-09-23";
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
System.out.println(publishDate);

Output :

Mon Sep 23 00:00:00 GMT 2013

Read the section Date and Time Patterns.

Upvotes: 8

Related Questions