How to transform currentTimeMillis to a readable date format?

Purplemonkey picture Purplemonkey · Apr 28, 2012 · Viewed 134.8k times · Source

I want to use currentTimeMillis twice so I can calculate a duration but I also want to display Time and Date in user readable format. I'm having trouble as currentTimeMillis is good for the calculation but I can't see a built in function to convert to nice time or time/date.

I use

android.text.format.DateFormat df = new android.text.format.DateFormat();
df.format("yyyy-MM-dd kk:mm:ss", new java.util.Date());

for producing nice time and date and what I'd ultimately like to do is show my resulting currentTimeMillis value into the android.text.format.DateFormat df = new android.text.format.DateFormat();

e.g.

android.text.format.DateFormat df = currentTimeMillis();

when I try I get

Type mismatch: cannot convert from long to DateFormat

I've tried to use some casting but can't see how to accomplish this.

Answer

asish picture asish · Apr 28, 2012

It will work.

long yourmilliseconds = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");    
Date resultdate = new Date(yourmilliseconds);
System.out.println(sdf.format(resultdate));