Ryan Malhotra
Ryan Malhotra

Reputation: 305

Java : Parsing Date String using SimpleDateFormat

My date string is like this dd.MM.yyyy-HH.mm.ss . I am doing following:

String s_date= "13.06.2012-12.12.12"
Date d_date = new SimpleDateFormat("dd.MM.YYYY-HH.mm.ss", Locale.ENGLISH).parse(s_date);    

But it is throwing Unparseable date: "13.06.2012-12.12.12" Exception.

How can I make it work for the given date-time format ?

Upvotes: 0

Views: 192

Answers (5)

KhAn SaAb
KhAn SaAb

Reputation: 5376

do like this.

String d=new SimpleDateFormat("dd.MM.yyyy").format(date);
System.out.println(d);

Upvotes: 0

Ofir Luzon
Ofir Luzon

Reputation: 10947

You are using capital Y.
Try:

Date d_date = new SimpleDateFormat("dd.MM.yyyy-HH.mm.ss", Locale.ENGLISH).parse(s_date);

Reference

Upvotes: 2

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35597

String s_date= ""13.06.2012-12.12.12";

this is wrong

use String s_date= "13.06.2012-12.12.12";

Upvotes: 0

Adam Sznajder
Adam Sznajder

Reputation: 9216

String s_date= "13.06.2012-12.12.12" doesn't fit your pattern dd.MM.YYYY. You should remove the part after the - if you want date without hours:

s_date = s_date.substring(0, s_date.indexOf('-'));

or change your pattern as Michał said.

Upvotes: 1

Michal Borek
Michal Borek

Reputation: 4634

You should add time as well:

 new SimpleDateFormat("dd.MM.YYYY-HH.mm.ss", Locale.ENGLISH)

Upvotes: 2

Related Questions