简体   繁体   中英

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

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

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.

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..

So at the end your options are:

  1. Re throw the exception

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);
}
  1. Use the properly try catch

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 :

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM