[英]Parse and format Date in Android
I'm having a problem in parsing a date. 我在解析日期时遇到问题。 I'm new on android and try to search for the solution but it seems no luck.
我是android的新手,尝试搜索解决方案,但似乎没有运气。 I'd already tried to follow this one http://developer.android.com/reference/java/text/SimpleDateFormat.html but still got an error.
我已经尝试遵循此http://developer.android.com/reference/java/text/SimpleDateFormat.html,但仍然出现错误。 please help me..
请帮我..
I try to parse this date 2014-03-18T02:07:35.742-0400
and try to format to this 03/18/2014 02:07
我尝试解析此日期
2014-03-18T02:07:35.742-0400
并尝试格式化为2014-03-18T02:07:35.742-0400
03/18/2014 02:07
I got this error: 我收到此错误:
java.lang.IllegalArgumentException
at java.text.DateFormat.format(DateFormat.java:361)
at java.text.Format.format(Format.java:93)
Try something like this: 尝试这样的事情:
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2014-03-18T02:07:35.742-0400");
System.out.println(new SimpleDateFormat("MM/dd/yyyy HH:mm").format(date));
For my time zone prints: 对于我的时区打印:
03/18/2014 10:07
2014/03/18 10:07
private final DateFormat parsedFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.getDefault());
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
Date date = dateFormat.parse(dateFormat.format(your_date));
Date parsedDate = parsedFormat.parse(parsedFormat.format(date));
use following code 使用以下代码
try{
String srcDate = new String("2014-03-18T02:07:35.742-0400");
SimpleDateFormat srcDf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat destDf = new SimpleDateFormat("yyyy/MM/dd");
Date date = srcDf.parse(srcDate);
// format the date into another format
dateStr = destDf.format(date);
}catch (ParseException e) {
// TODO: handle exception
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.