如何转换“Mon Jun 18 00:00:00 IST 2012"到 18/06/2012? [英] How to convert "Mon Jun 18 00:00:00 IST 2012" to 18/06/2012?
本文介绍了如何转换“Mon Jun 18 00:00:00 IST 2012"到 18/06/2012?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似于以下的值 Mon Jun 18 00:00:00 IST 2012
我想将其转换为 18/06/2012
I have a value like the following Mon Jun 18 00:00:00 IST 2012
and I want to convert this to 18/06/2012
如何转换?
我试过这个方法
public String toDate(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date theDate = null;
//String in = date + "/" + month + "/" + year;
try {
theDate = dateFormat.parse(date.toString());
System.out.println("Date parsed = " + dateFormat.format(theDate));
} catch (ParseException e) {
e.printStackTrace();
}
return dateFormat.format(theDate);
}
但它抛出以下异常:
java.text.ParseException:无法解析的日期:Mon Jun 18 00:00:00 IST 2012"
推荐答案
希望下面的程序能解决你的问题
I hope following program will solve your problem
String dateStr = "Mon Jun 18 00:00:00 IST 2012";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(dateStr);
System.out.println(date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String formatedDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
System.out.println("formatedDate : " + formatedDate);
这篇关于如何转换“Mon Jun 18 00:00:00 IST 2012"到 18/06/2012?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文