0

I'm making an app, that is storing some dates in a SQLite db on an android device.

Currently, most of it works as intended, except for parsing the text string that i store the date as.

    private final String ALARMS_COLUMN_TIME = "time";

    Calendar cal = Calendar.getInstance();
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      String dateString = cursor.getString(cursor.getColumnIndex(ALARMS_COLUMN_TIME));
      cal.setTime(dateFormat.parse(dateString));

The problem is that, even before compiling, it gives me a, seemingly syntax error, with the "unhandled exception java.text.ParseException".

the imports i'm using in that class are these:

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;

I have tried with various Locales as well, as part of the SimpleDateFormat constructor, but it haven't made any difference.

What can be the cause of this unhandled exception, prior to compiling?

Thanks in advance,

Ronnie

Ronin
  • 65
  • 1
  • 9
  • I'm pretty sure / works when using SimpleDateFormat to turn a calendar object/long time into readable text. But i'l try to change the /'s Edit: Gives same unhandled exception error – Ronin Nov 04 '15 at 10:33
  • 1
    what is the value of `dateString` ? – ThomasThiebaud Nov 04 '15 at 10:35
  • The Value of dateString will be a date in the format identical to the format used in the SimpleDateFormat constructor. The exception is shown in android studio, prior to compiling Edit: so what the actual value of dateString is, should not be a reason for the IDE to mark it as an exception, as it can't see what it possibly could be in the database – Ronin Nov 04 '15 at 10:37

1 Answers1

3

It's not that a ParseException is being thrown - the problem is that the compiler is complaining because you're calling parse which can throw a ParseException, and you're not handling it.

ParseException is a checked exception, which means that if you call a method that is declared to throw it, then you either need to catch it yourself, or you need to declare that your method might throw it. (We can't tell from your code which of those you want to do. You might want to catch it and rethrown an unchecked exception, for example.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Yvette: How could that be relevant when this is a *compile-time* error? – Jon Skeet Nov 04 '15 at 10:40
  • That makes sense i guess, i had no clue about 'checked exception' and such. Thanks, i'l put it in a try-catch! Accepting answer in a few minutes, when SO will let me =) – Ronin Nov 04 '15 at 10:41