Samir Mangroliya
Samir Mangroliya

Reputation: 40416

Android /java Time Format?

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);

Upvotes: 2

Views: 1236

Answers (1)

Vaandu
Vaandu

Reputation: 4935

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

Upvotes: 7

Related Questions