tags:

views:

2586

answers:

5
long lastmodified = file.lastModified();
String lasmod =  /*TODO: Transform it to this format YYYY-MM-DD*/
+12  A: 

Something like:

Date lm = new Date(lastmodified);
String lasmod = new SimpleDateFormat("yyyy-MM-dd").format(lm);

See the javadoc for SimpleDateFormat.

sblundy
Just to state the obvious here, if you're doing this in the context of a long-lived object or in a loop, you probably want to construct the SimpleDateFormat object once and reuse it.
nsayer
Although beware - SimpleDateFormat is not thread-safe
oxbow_lakes
+2  A: 
String lasmod = new SimpleDateFormat("yyyy-MM-dd").format(new Date(lastmodified));

Look up the correct pattern you want for SimpleDateFormat... I may have included the wrong one from memory.

Instantsoup
A: 
Date d = new Date(lastmodified);
DateFormat form = new SimpleDateFormat("yyyy-MM-dd");
String lasmod = form.format(d);
Paul Tomblin
Lower case 'mm' is minute.
sblundy
+2  A: 
final Date modDate = new Date(lastmodified);
final SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
final String lasmod = f.format(modDate);

SimpleDateFormat

Lars Westergren
+1  A: 

Try:

import java.text.SimpleDateFormat;
import java.util.Date;

long lastmodified = file.lastModified();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String lastmod =  format.format(new Date(lastmodified));
James Cooper