问题
How to convert 2013-06-24 to 24 Jun 2013? I am using the below code.
date1="2013-06-24";
SimpleDateFormat d= new SimpleDateFormat("dd MMM yyyy");
try{
date2 = d.parse(date1);
}catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
But I am getting this error "java.text.ParseException: Unparseable date: "2013-06-24" (at offset 4)"
回答1:
You need two DateFormat
instances: One to parse the original String
, and another to output the one you want.
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String inputDateStr="2013-06-24";
Date date = inputFormat.parse(inputDateStr);
String outputDateStr = outputFormat.format(date);
回答2:
See the first problem is that you are using different delimiters for String and Date. So either you do "2013-06-24" to "2013 06 24" in String or do new SimpleDateFormat("dd MMM yyyy") to new SimpleDateFormat("dd-MMM-yyyy").
And second problem is that you cannot directly change format like this, in String you are having year-month-date format, so first make a Date object with same format than change it to your desired format as below :
date1="2013-06-24";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date dt = format.parse(date1);
SimpleDateFormat your_format = new SimpleDateFormat("dd-MMM-yyyy");
date2 = your_format.format(dt);
回答3:
Change
SimpleDateFormat d= new SimpleDateFormat("dd MMM yyyy");
with
SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");
you have to follow the date1 pattern. Then you can format your parsed date with
new SimpleDateFormat("dd MMM yyyy");
回答4:
public String getStringFormatted(String datestring) {
String format = "dd MM yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
return sdf.format(new Date(datestring.replaceAll("-", "/")));
}
回答5:
public String inputFormatget (String datestring1) {
String format = "dd MM yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
return sdf.format(new Date(datestring.replaceAll("-", "/")));
}
public String outFormatset (String datestring2) {
String format = "dd MM yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
return sdf.format(new Date(datestring.replaceAll("-", "/")));
}
回答6:
public static String formateDateFromstring(String inputFormat, String outputFormat, String inputDate){
Date parsed = null;
String outputDate = "";
SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());
try {
parsed = df_input.parse(inputDate);
outputDate = df_output.format(parsed);
} catch (ParseException e) {
}
return outputDate;
}
String convertedDate = formateDateFromstring(
"yyyy-MM-dd",
"dd/MM/yyyy",
datestring);
updateInsuranceEventFragmentBinding.edtdate.setText(convertedDate);
来源:https://stackoverflow.com/questions/17324060/converting-yyyy-mm-dd-into-dd-mm-yyyy