[英]How to do Date parsing and formating inside a bindview of cursor adapter in android
I have saved my dates in my sqlite table like 2016-04-20 and I want my listview display them as 20/4/2016 and I use the following inside the bindview of a cursor adapter 我已经将日期保存在像2016-04-20这样的sqlite表中,并且希望列表视图将日期显示为20/4/2016,并且在游标适配器的bindview中使用以下内容
String InitialDate=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2)));
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd");
Date dateObj = curFormater.parse(InitialDate);
SimpleDateFormat postFormater = new SimpleDateFormat("dd/MM/yyyy");
String newDateStr = postFormater.format(dateObj);
textViewDate.setText(newDateStr);
but before I do anything the part of parse says Unhandled Exception java.text.ParseException I have import of 但是在我做任何事情之前,解析的一部分说未处理的异常java.text.ParseException我已经导入了
import java.text.SimpleDateFormat;
import java.util.Date;
These is because the method you are calling can throw an exception and you are not taking care of it.. look: 这是因为您正在调用的方法可能会引发异常,并且您没有在意它。
Date java.text.DateFormat.parse(String source) throws ParseException
Throws: ParseException - if the beginning of the specified string cannot be parsed. 抛出:ParseException-如果无法分析指定字符串的开头。
This basically means, if you try to convert a non-sense into a date, the java.text.DateFormat
class will try its best to but if not possible will throw the exception, that you must either catch using the properly try-catch statement or just re throwing the exception so others can take care of it.. 这基本上意味着,如果您尝试将无意义的日期转换为日期,则java.text.DateFormat
类将尽力而为,但如果不可能,将引发异常,您必须使用正确的try-catch语句进行捕获或者只是抛出异常,以便其他人可以处理它。
t Ť
public static void main(String[] args) throws ParseException {
String InitialDate = new String("2016-04-20");
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd");
Date dateObj = curFormater.parse(InitialDate);
SimpleDateFormat postFormater = new SimpleDateFormat("dd/MM/yyyy");
String newDateStr = postFormater.format(dateObj);
}
Use.... 采用....
public static void main(String[] args) {
String InitialDate = new String("2016-04-20");
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd");
Date dateObj;
try {
dateObj = curFormater.parse(InitialDate);
SimpleDateFormat postFormater = new SimpleDateFormat("dd/MM/yyyy");
String newDateStr = postFormater.format(dateObj);
} catch (ParseException e) {
e.printStackTrace();
System.err.println("a non sense was here");
}
}
you should use try/catch block for parse
method call because it can produce checked exception : 您应该使用try / catch块进行parse
方法调用,因为它可能产生已检查的异常 :
Date dateObj = null;
try {
dateObj = curFormater.parse(InitialDate);
} catch (ParseException e) {
e.printStackTrace();
}
or you can just rethrow this exception from your method (you should use throws ParseException
clause in method signature) 或者您可以从方法中抛出该异常(您应该在方法签名中使用throws ParseException
子句)
via documentation : 通过文档 :
Checked exceptions are subject to the Catch or Specify Requirement. 已检查的异常受“捕获”或“指定要求”的约束。 All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses. 除Error,RuntimeException及其子类指示的那些异常外,所有异常都是经过检查的异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.