搜索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)
谢谢.
比较原语时,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)
| 归档时间: |
|
| 查看次数: |
4336 次 |
| 最近记录: |