Question

How can I transform a time value into YYYY-MM-DD format in Java?

long lastmodified = file.lastModified();
String lasmod =  /*TODO: Transform it to this format YYYY-MM-DD*/
Was it helpful?

Solution

Something like:

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

See the javadoc for SimpleDateFormat.

OTHER TIPS

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

SimpleDateFormat

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.

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));
Date d = new Date(lastmodified);
DateFormat form = new SimpleDateFormat("yyyy-MM-dd");
String lasmod = form.format(d);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top