1、例子:
String MonthYear = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm/yyyy");
String dateformat = "2012-11-17T00:00:00.000-05:00"
MonthYear = simpleDateFormat.format(dateformat);
System.out.println(MonthYear);
2、解决方法:(这个错误的原因就是说,String类型无法直接parse为一个date类型,所以需要先把String字符串,格式化为一个datea类型,最后再格式化为你想要的格式)
DateFormat outputFormat = new SimpleDateFormat("yyyy/MM");
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Date date = null;
String inputText = "2012-11-17";
try {
date = inputFormat.parse(inputText);
}catch (Exception e){
e.printStackTrace();
}
String outputText = outputFormat.format(date);
System.out.println(outputText);
本文介绍了一种将特定格式的日期字符串转换成另一种格式的方法。通过使用SimpleDateFormat类,可以实现从一种日期格式到另一种格式的有效转换。具体步骤包括定义输入和输出格式、解析原始字符串并重新格式化。
1万+

被折叠的 条评论
为什么被折叠?



