我试图在android应用程序中将字符串解析为日期字段,但似乎无法正确处理。下面是我试图转换为日期"03/26/2012 11:49:00 AM“的字符串。我使用的函数是:
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:49AM
作为结果。
发布于 2012-03-30 22:40:18
我猜你显示数据的方式是错误的,因为对我来说:
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
发布于 2013-09-21 14:48:02
当我在SimpleDateFormat
中使用Locale.US
参数时,它运行正常
String dateString = "15 May 2013 17:38:34 +0300";
System.out.println(dateString);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.US);
DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
String formattedDate = null;
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
System.out.println(dateString);
formattedDate = targetFormat.format(convertedDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
发布于 2012-03-30 22:50:06
String str_date="13-09-2011";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MM-yyyy");
date = (Date)formatter.parse(str_date);
System.out.println("Today is " +date.getTime());
尝尝这个
https://stackoverflow.com/questions/9945072
复制相似问题