sam
sam

Reputation: 69

how to parse a string in to date,excluding time

i want to parse a string into date having the below code, but output contains time also.
I don't want time in my output, I just want date.

public static void main(String args[]){
String givendate="2013-09-09"; 
Date date=(new SimpleDateFormat("yyyy-MM-dd").parse(givendate));
System.out.println(date);
}

Output of the program-: Mon Sep 09 00:00:00 IST 2013

Upvotes: 1

Views: 123

Answers (3)

Marcin Szymczak
Marcin Szymczak

Reputation: 11443

Use Joda Time

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime dt = formatter.parseDateTime(string);

Upvotes: 0

user2762786
user2762786

Reputation: 11

The issue here is that when printing you are invoking the default format for printing the date object which includes time. Check out this link for the various formatting options

http://www.tutorialspoint.com/java/java_date_time.htm

Upvotes: 1

Claudiu
Claudiu

Reputation: 1489

Try using the same formatter when printing:

System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));

implicitely a Date object contains time too.

Upvotes: 1

Related Questions