Adil Bhatty
Adil Bhatty

Reputation: 17340

How to change date's format from milliseconds

I am receiving last modification date of a file, using below code:

xmlUrl = new URL("http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html");
URLConnection urlconn = xmlUrl.openConnection();
urlDate =  new Date(urlconn.getLastModified());

In result I am getting date in below format:

Tue Dec 18 05:11:33 Asia/Karachi 2007

I want to change it to simple dd MMM yyyy format

I used:

SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy");
try {
        tempDate = format.parse(urlDate.toString());

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

but it didn't help me to solve the issue and I am still getting the date in that above mentioned long format.

Upvotes: 0

Views: 395

Answers (1)

Michael Borgwardt
Michael Borgwardt

Reputation: 346347

tempDate = format.parse(urlDate.toString());

That's backwards and should lead to an exception. A DateFormat is for converting between String and Date both ways, and the format string must always match the pattern of the String side.

What you want is this:

tempDate = format.format(urlDate);

Upvotes: 5

Related Questions