简体   繁体   English

SimpleDateFormat在编译之前在AndroidStudio中引发ParseException

[英]SimpleDateFormat throws ParseException in AndroidStudio before compiling

I'm making an app, that is storing some dates in a SQLite db on an android device. 我正在制作一个应用程序,它将一些日期存储在Android设备上的SQLite数据库中。

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". 问题是,即使在编译之前,它也会给我一个看似语法上的错误,并带有“未处理的异常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. 作为SimpleDateFormat构造函数的一部分,我也尝试了各种语言环境,但没有任何区别。

What can be the cause of this unhandled exception, prior to compiling? 在编译之前,此未处理异常的原因是什么?

Thanks in advance, 提前致谢,

Ronnie 罗尼

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 抛出-问题是,编译器抱怨,因为你调用parse可以抛出一个ParseException ,而你不处理它。

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. ParseException是一个已检查的异常 ,这意味着如果您调用一个声明为抛出它的方法,则您要么需要自己捕获它,要么需要声明您的方法可能抛出它。 (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.) (例如,我们无法从您的代码中确定要执行的操作。例如,您可能想捕获它并重新抛出未经检查的异常。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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