Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

time

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*/
like image 549
Sergio del Amo Avatar asked Oct 22 '08 16:10

Sergio del Amo


People also ask

What is YYYY-mm-DD format?

In data processing, the year, month and day information are usually written as yyyymmdd, where the first four digits are Year, the fifth and sixth digits are Month, and the last two digits are Day. For example, 19710428 means April 8, 1971, and 20000101 means January 1, 2000.


2 Answers

Something like:

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

See the javadoc for SimpleDateFormat.

like image 54
sblundy Avatar answered Oct 13 '22 22:10

sblundy


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

SimpleDateFormat

like image 4
Lars Westergren Avatar answered Oct 13 '22 22:10

Lars Westergren