将一个日期字符串转换为另一个java
我环顾四周,找不到任何符合我需要的东西 我得到以下字符串:将一个日期字符串转换为另一个java,java,string,simpledateformat,Java,String,Simpledateformat,我环顾四周,找不到任何符合我需要的东西 我得到以下字符串: "March 13, 2013" 需要将其转换为 "2013-03-13" 我尝试使用以下代码: SimpleDateFormat inputFormat = new SimpleDateFormat("MMMMMMMMMM dd, YYYY"); SimpleDateFormat outputFormat = new SimpleDateFormat("YYYY-MM-dd"); Date d = inpu
"March 13, 2013"
需要将其转换为
"2013-03-13"
我尝试使用以下代码:
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMMMMMMMM dd, YYYY");
SimpleDateFormat outputFormat = new SimpleDateFormat("YYYY-MM-dd");
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));
但我得到的是“203-12-30”。。为什么会这样?
Y
表示一周或一年。尝试使用y
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Y
表示周和年。尝试使用y
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
请尝试使用以下代码:
String startDate = "March 13, 2013";
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMM dd, yyyy",Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));
请尝试使用以下代码:
String startDate = "March 13, 2013";
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMM dd, yyyy",Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));
我只是复制并粘贴了你的代码,做了一些小改动,得到了你想要的结果。我所做的主要更改是将inputFormat中的大写字母“Y”和outputFormat更改为小写字母“Y”。不管怎样,给你:
String startDate = "March 13, 2013";
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMMMMMMMM dd, yyyy");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
我只是复制并粘贴了你的代码,做了一些小改动,得到了你想要的结果。我所做的主要更改是将inputFormat中的大写字母“Y”和outputFormat更改为小写字母“Y”。不管怎样,给你:
String startDate = "March 13, 2013";
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMMMMMMMM dd, yyyy");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));
} catch (ParseException e) {
e.printStackTrace();
}