0

I am trying to format a date such as Mon Jul 28 00:00:00 CDT 2014 to something a bit more user friendly, such as Mon 6/28. What am I doing wrong?

Code:

        String date_string = "Mon Jul 28 00:00:00 CDT 2014";

        SimpleDateFormat inputFormatter = new SimpleDateFormat("ccc LLL F HH:mm:ss zzz yyyy"); //please notice the    capital M
        SimpleDateFormat outputFormatter = new SimpleDateFormat("ccc LLL/yyyy");

        try {
            Date date = inputFormatter.parse(date_string);
            String text = outputFormatter.format(date);
            System.out.println(text);
        } catch (ParseException e) {
            e.printStackTrace();
        }

When I use System.out.println to see what outputFormatter is set to, the result is: Sun Jan/2015

moatist
  • 198
  • 1
  • 13
  • Can you see the connection between `ccc LLL/yyyy` & `Sun Jan/2015`? Check out [the docs](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). – takendarkk Jun 30 '14 at 05:02
  • check this so you can see the actual pattern letters http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Ed Morales Jun 30 '14 at 05:03
  • I'm sorry, is this a stupid question? Not sure why I got downvoted... I'll look at the docs. – moatist Jun 30 '14 at 05:48
  • possible duplicate of [How do you format date and time in Android?](http://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android) – Basil Bourque Jun 30 '14 at 07:10

4 Answers4

3

Try this..

Change this..

    SimpleDateFormat outputFormatter = new SimpleDateFormat("ccc LLL/yyyy");

to

    SimpleDateFormat outputFormatter = new SimpleDateFormat("EEE M/dd");

EEE --> Mon

EEEE --> Monday

MM --> 06

M --> 6

dd --> 28

EDIT

    SimpleDateFormat inputFormatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

DEMO

http://ideone.com/c5rVT5

Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

"Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number."

If you want the number version, use the shorter format in your output.

Phuong Nguyen
  • 909
  • 7
  • 20
0

Try this

DateFormat df = new SimpleDateFormat("EEE M/dd");

Check this and this for more details.

Aniruddha
  • 4,477
  • 2
  • 21
  • 39
0
SimpleDateFormat inputFormatter = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
SimpleDateFormat outputFormatter = new SimpleDateFormat("EEE MMM/yyyy");

This will print : Mon Jul/2014

If you use :

SimpleDateFormat outputFormatter = new SimpleDateFormat("EEE L/d");

This will print : Mon 7/28