2

I have time as a "2011-12-03 12:00:19" how to convert it in "Fri 2 December 2011 " ,I know this http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html ,But gives me Error:

 Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
    at java.text.DateFormat.format(Unknown Source)
    at java.text.Format.format(Unknown Source)
    at com.timestamp.NewTimeStamp.<init>(NewTimeStamp.java:21)
    at com.timestamp.NewTimeStamp.main(NewTimeStamp.java:35)

My code is ::

String mytime ="2011-12-03 12:00:19";
String pattern = "EEE d MMMMM yyyy";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);

        Date date = new Date(mytime);
        String time = dateFormat.format(date);

        System.out.println("=== > " + time);
2
  • String pattern = "EEE d MMMMM yyyy"; Commented Jan 2, 2012 at 10:15
  • 2
    Date date = new Date(mytime); I think this line is giving you the error. put debug code System.out.println ( "1" ) & ( "2" ) before & after that line & then execute & see if that line really generates the error or what ? Commented Jan 2, 2012 at 10:17

1 Answer 1

7

Convert your input to Date and then format.

        String mytime ="2011-12-03 12:00:19";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
        Date myDate = dateFormat.parse(mytime);
        System.out.println("=== > " + myDate);
        SimpleDateFormat timeFormat = new SimpleDateFormat("EEE d MMMMM yyyy");
        String time = timeFormat.format(myDate);
        System.out.println("=== > " + time);

Output:

D:\Work\Stand alone Java classes>javac Test2.java && java Test2
=== > Wed Jan 12 12:00:19 IST 2011
=== > Wed 12 January 2011
Sign up to request clarification or add additional context in comments.

2 Comments

But wht abt month its return january actually it is december?
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss"); is right

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.