[英]SimpleDateFormat.parse(String) gives null value android
I am working on android application in which i am parsing date and time in UTC by using SimpleDateFormat
but the problem is that on parsing that string i am getting the null value. 我正在使用通过SimpleDateFormat
在UTC中解析日期和时间的android应用程序,但问题是在解析该字符串时我得到了null值。 My code is given below, along with the date which i got from date pickers. 下面给出了我的代码,以及我从日期选择器获得的日期。
// It is the date which i get it from pickers mDateSelected = 6/24/2015 4:07:00 PM
Date myDate = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
// Here i am getting null Value ar myDate
myDate = simpleDateFormat.parse(mDateSelected);
} catch (Exception e1) {
e1.printStackTrace();
}
your SimpleDateFormat
pattern is wrong. 您的SimpleDateFormat
模式错误。 Since you have a date in the format 由于您的日期格式
6/24/2015 4:07:00 PM
it should be 它应该是
m/dd/yyyy h:mm:ss a
and not 并不是
"yyyy-MM-dd HH:mm:ss"
use these code following below 使用以下代码
String Todaydate;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Todaydate = df.format(new java.util.Date());
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Log.d("Current Date", Todaydate);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.