比较字符串日期与当前长时间

Akh*_*tta 1 java android

搜索API以String格式返回日期,我想将该日期与当前日期进行比较并执行某些操作.我创建了一个日期对象并对其进行了解析,但在进行比较时仍然出现错误.

Calendar currentDate = Calendar.getInstance();
long dateNow = currentDate.getTimeInMillis();

String eventDate = meta.getString("startDate"); //This is the string API returns

SimpleDateFormat formatter  = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS");
Date date = formatter.parse(eventDate);
long modifiedDate= date.getTime();

if (dateNow.compareTo(modifiedDate)>0) {
    //Do Something
}                            
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

Cannot invoke compareTo(long) on the primitive type long
Run Code Online (Sandbox Code Playgroud)

谢谢.

cre*_*ama 5

比较原语时,long只需使用一个vanilla比较运算符:

dateNow > modifiedDate
Run Code Online (Sandbox Code Playgroud)

建议不要这样做,但如果要使用compareTo,请先转换为Long:

Long.valueOf(dateNow).compareTo(Long.valueOf(modifiedDate)) > 0
Run Code Online (Sandbox Code Playgroud)