将字符串转换为Java中的日期 [英] Convert string to Date in java
本文介绍了将字符串转换为Java中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图解析字符串到日期字段在一个Android应用程序,但我似乎无法得到它正确。这是我想要转换为日期2012年3月26日上午11点49分○○秒的字符串。我使用的功能是:
I'm trying to parse a string to a date field in an android application but I can't seem to get it correct. Here is the string I'm trying to convert to a date "03/26/2012 11:49:00 AM". The function I'm using is:
private Date ConvertToDate(String dateString){
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertedDate;
}
不过,我不断收到3/1/112上午11点49分的结果..任何帮助,我真的AP preciate。谢谢
But I keep getting 3/1/112 11:49AM as the result.. Any help I would really appreciate. thanks
推荐答案
您是错在你显示我猜的数据的方式,因为对我来说:
You are wrong in the way you display the data I guess, because for me:
String dateString = "03/26/2012 11:49:00 AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
打印:
Mon Mar 26 11:49:00 EEST 2012
这篇关于将字符串转换为Java中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文