如何在 Java 中将时间值转换为 YYYY-MM-DD 格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/226618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:42:56 来源:igfitidea点击:
How to transform a time value into YYYY-MM-DD format in Java?
提问by Sergio del Amo
How can I transform a time value into YYYY-MM-DD format in Java?
如何在 Java 中将时间值转换为 YYYY-MM-DD 格式?
long lastmodified = file.lastModified();
String lasmod = /*TODO: Transform it to this format YYYY-MM-DD*/
采纳答案by sblundy
Something like:
就像是:
Date lm = new Date(lastmodified);
String lasmod = new SimpleDateFormat("yyyy-MM-dd").format(lm);
See the javadoc for SimpleDateFormat.
请参阅SimpleDateFormat的 javadoc 。
回答by Instantsoup
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.
为 SimpleDateFormat 查找您想要的正确模式...我可能从内存中包含了错误的模式。
回答by Paul Tomblin
Date d = new Date(lastmodified);
DateFormat form = new SimpleDateFormat("yyyy-MM-dd");
String lasmod = form.format(d);
回答by Lars Westergren
final Date modDate = new Date(lastmodified);
final SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
final String lasmod = f.format(modDate);
回答by James Cooper
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));