1

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

2
  • 1
    You didn't format the output. Commented Sep 9, 2013 at 20:48
  • Date is a container for the number of milliseconds since the Unix epoch, it has no concept of a format. Date#toString simply dumps informational output about the contents of the Date Object. You can't remove the time from a Date object any more then you can remove the Date, but you can format it, try using the same or new formatter that meets you needs to change the way that the Date is displayed Commented Sep 9, 2013 at 22:56

3 Answers 3

1

Try using the same formatter when printing:

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

implicitely a Date object contains time too.

Sign up to request clarification or add additional context in comments.

4 Comments

I would think it contains time. What you should say is Date contains a default format.
sir i dont wanna print it.i want to parse it and wanna put parse result in to text field.when i use three abbreviations of "M"("yyyy-MMM-dd") it shows error Exception in thread "main" java.text.ParseException: Unparseable date: "2013-09-09" at java.text.DateFormat.parse(DateFormat.java:357)
when i use two "M" abbreviations it parses but output contains time too....output of program when two "M" abbreviations used- Mon Sep 09 00:00:00 IST 2013
@user2281498 A text field contains a String, so you will need to use SimpleDateFormat#format() to convert your Date to a String. If you simply System.out.println(date), you will get a default format for the date, which includes the time.
1

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

Comments

0

Use Joda Time

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.