【发布时间】:2017-01-20 09:54:06
【问题描述】:
我有一个下面的程序来比较两个日期。
我得到的时间戳是 date1 和 currentTimestamp,在这里我只需要比较日期而不是时间值。但下面的程序总是返回 -1。(值)
timestamp date1 = "2017-01-20 14:51:30.091" // i get this from service call in this format
SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_TIMESTAMP_FORMAT);
Calendar calendar = Calendar.getInstance();
String formattedDate = dateFormat.format(calendar.getTime());
java.util.Date currentDate = dateFormat.parse(formattedDate);
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(currentDate.getTime());
int value = DateTimeComparator.getDateOnlyInstance().compare(date1 , currentTimestamp );
如何只比较日期而不考虑时间。请帮我解决这个问题。
更新:
我改成下面的代码
timestamp date1 = "2017-01-20 14:51:30.091" // i get this from service call in this format
LocalDate localDate = new LocalDate();
int value = DateTimeComparator.getDateOnlyInstance().compare(date1 , localDate );
这给了我错误提示“java.lang.IllegalArgumentException: No Instant Converter found for type: org.joda.time.LocalDate”
【问题讨论】:
-
嗯。要使用 Joda 时间,请不要使用
Calendar、SimpleDateFormat、java.util.Date或java.sql.Timestamp。 仅使用Joda-time classes。 -
也许我看错了,但你发布的代码中 Joda 在哪里??
-
@RealSkeptic 我更改为 localdate 以获取当前日期,但它给出错误“java.lang.IllegalArgumentException: No instant converter found for type: org.joda.time.LocalDate”
-
什么是
timestamp?看起来它应该是某种类或类型名称,但是您要为其分配一个字符串?请发布真实代码。如前所述,minimal reproducible example 是最好的。