格式日期Java [英] Format Date Java
问题描述
如何格式化一个这样的字符串
How to format a string that looks like this
Sat Dec 08 00:00:00 JST 2012
Sat Dec 08 00:00:00 JST 2012
into yyyy-mm-dd ie
into yyyy-mm-dd i.e.
2012-12- 08
2012-12-08
浏览 web ,我发现这段代码:
From browsing the web, I found this piece of code:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "Sat Dec 08 00:00:00 JST 2012";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
但是,我无法修改它接受第一行(Sat Dec 08 00:00:00 JST 2012)作为字符串和格式,以yyyy-mm-dd格式。
However, I am unable to modify it to accept the first line (Sat Dec 08 00:00:00 JST 2012) as a string and format that into the yyyy-mm-dd format.
我该怎么办?我应该尝试修改吗?或者尝试另一种方法?
What should I do about this? Should I be attempting to modify this? Or try another approach altogether?
更新:我使用这个从你的答案(得到错误:不可稀释的日期:星期六12月08 00:00:00 JST 2012)
Update: I'm using this from your answers (getting error: Unparseable date: "Sat Dec 08 00:00:00 JST 2012")
public static void main(String[] args) throws ParseException{
SimpleDateFormat srcFormatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.JAPANESE);
SimpleDateFormat destFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.JAPANESE);
Date date = srcFormatter.parse("Sat Dec 08 00:00:00 JST 2012");
String destDateString = destFormatter.format(date);
/* String dateInString = "Sat Dec 08 00:00:00 JST 2012";*/
System.out.println(destDateString);
/*try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}*/
}
}
推荐答案
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String dateInString = "Wed Oct 16 00:00:00 CEST 2013";
try {
SimpleDateFormat parse = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date date = parse.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
将您的格式更改为新的SimpleDateFormat(yyyy -MM-dd,Locale.ENGLISH);
谢谢..
这篇关于格式日期Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!