Chirag
Chirag

Reputation: 56935

How can I format date?

I have dates in the format:

Wed Jan 18 08:50:04 Asia/Calcutta 2012

I want to depict just 18.01.2012, how do I do this?

Upvotes: 1

Views: 93

Answers (2)

Siten
Siten

Reputation: 4533

Try in your code:

SimpleDateFormat sdfDate = new SimpleDateFormat("dd.MM.yyyy");
            java.util.Date date = new java.util.Date();
            String strDate = sdfDate.format(date);

I tried that.

Upvotes: 0

st0le
st0le

Reputation: 33565

A Bit hacky because of the Asia/Calcultta

EDIT: Also found this

public class Foo {
    public static void main(String[] args) throws Exception {
        String date = "Wed Jan 18 08:50:04 Asia/Calcutta 2012";
        System.out.println(formatDate(date));
    }

    private static String formatDate(String date) throws Exception {
        String year = date.substring(date.lastIndexOf(' ') + 1);
        DateFormat sdf = new SimpleDateFormat("EEE MMM dd kk:mm:ss");
        SimpleDateFormat fmt = new SimpleDateFormat(String.format("dd.MM.%s",
                                                                  year));
        Date parse = sdf.parse(date);
        return fmt.format(parse);
    }
}

Upvotes: 2

Related Questions