如何将日期转换为 yyyy-MM-dd 格式? [英] How to convert date in to yyyy-MM-dd Format?
本文介绍了如何将日期转换为 yyyy-MM-dd 格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
2012 年 12 月 1 日星期六 00:00:00 GMT
Sat Dec 01 00:00:00 GMT 2012
我必须将上面的日期转换成下面的格式
I have to convert above date into below format
2012-12-01
我该怎么办?
我尝试了以下方法但它不起作用
i have tried with following method but its not working
public Date ConvertDate(Date date){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String s = df.format(date);
String result = s;
try {
date=df.parse(result);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
推荐答案
使用这个.
java.util.Date date = new Date("Sat Dec 01 00:00:00 GMT 2012");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String format = formatter.format(date);
System.out.println(format);
你会得到输出
2012-12-01
这篇关于如何将日期转换为 yyyy-MM-dd 格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文