How to convert time value to YYYY-MM-DD format in Java?

How to convert time value to YYYY-MM-DD format in Java?

long lastmodified = file.lastModified(); String lasmod = /*TODO: Transform it to this format YYYY-MM-DD*/ 
+7
java time
source share
5 answers

Something like:

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

See javadoc for SimpleDateFormat .

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

SimpleDateFormat

+4
source share
 String lasmod = new SimpleDateFormat("yyyy-MM-dd").format(new Date(lastmodified)); 

Check out the correct template you want for SimpleDateFormat ... Maybe I included the wrong one from memory.

+3
source share

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)); 
+1
source share
 Date d = new Date(lastmodified); DateFormat form = new SimpleDateFormat("yyyy-MM-dd"); String lasmod = form.format(d); 
0
source share

All Articles