将带有 Am/pm 制造商的字符串转换为 Java 中的日期
Convert string with Am/pm maker to date in Java
我想在 java 中将字符串 "10/7/2015 6:31am"
转换为 Date
。
我尝试了以下代码,但失败了。
String value = "10/7/2015 6:31am";
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy h:mma");
return sdf.parse(value);
我该用什么SimpleDateFormat
,求大神帮忙
其实我在Utils.java中做了一个函数如下:
public static Date getDateValueByFormat(String value,String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(value);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
在别人class中调用这个函数如下:
String value = "10/7/2015 6:31am";
Date datetime = Utils.getDateValueByFormat(value, "M/d/yyyy h:mma");
不幸的是,抛出异常如下:
java.text.ParseException: Unparseable date: "10/7/2015 6:31am"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.sapmle.common.util.Utils.getDateValueByFormat(Utils.java:28)
有人知道吗?
PS: 我用的jdk是1.7.0_79,有什么问题吗?
汤姆,prashant thakre,
非常感谢!
按照你的方法我成功了:
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.US);
我想在 java 中将字符串 "10/7/2015 6:31am"
转换为 Date
。
我尝试了以下代码,但失败了。
String value = "10/7/2015 6:31am";
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy h:mma");
return sdf.parse(value);
我该用什么SimpleDateFormat
,求大神帮忙
其实我在Utils.java中做了一个函数如下:
public static Date getDateValueByFormat(String value,String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(value);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
在别人class中调用这个函数如下:
String value = "10/7/2015 6:31am";
Date datetime = Utils.getDateValueByFormat(value, "M/d/yyyy h:mma");
不幸的是,抛出异常如下:
java.text.ParseException: Unparseable date: "10/7/2015 6:31am"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.sapmle.common.util.Utils.getDateValueByFormat(Utils.java:28)
有人知道吗?
PS: 我用的jdk是1.7.0_79,有什么问题吗?
汤姆,prashant thakre, 非常感谢!
按照你的方法我成功了:
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.US);