很抱歉再次发布同样的问题,但我很想知道。
我想知道如何将java中的当前日期与mysql中的存储日期进行比较。
存储在MySQL中的日期为2013-01-04 13:15:00
当我通过获取当前日期
来比较java中的这个日期时Date date = new Date();
然后写了一个查询,如果存储在MySQL中的日期小于当前显示的结果。但是查询在List中返回0结果,或者我在最后粘贴了异常。
select model from abc model where model.abcStartDate <= :? and model.abcEndDate >= :?
在“:?”日期过去了 在MySQL中,开始日期和结束日期是时间戳数据类型。
以下是EJB实体类
@Entity
@Table(name = "abc_table", uniqueConstraints = {})
public class ABCClass implements java.io.Serializable {
private static final long serialVersionUID = -1924961655993587546L;
// Fields
private Date abcStartDate;
private Date abcEndDate;
// Constructors
/** default constructor */
public ABCClass() {
}
@OrderBy(value="3")
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "abc_start_date", unique = false, nullable = false, insertable = true, updatable = true, length = 19)
public Date getabcStartDate() {
return this.abcStartDate;
}
public void setabcStartDate(Date abcStartDate) {
this.abcStartDate = abcStartDate;
}
@OrderBy(value="4")
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "abc_end_date", unique = false, nullable = false, insertable = true, updatable = true, length = 19)
public Date getabcEndDate() {
return this.abcEndDate;
}
public void setabcEndDate(Date abcEndDate) {
this.abcEndDate = abcEndDate;
}
}
并抛出异常
Exception thrown from bean; nested exception is: Exception [EclipseLink-3001] (Eclipse Persistence Services - 2.2.1.v20110722-r9776): org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [65464533565], of class [class java.lang.String], could not be converted to [class java.lang.Integer].
Internal Exception: java.lang.NumberFormatException: For input string: "65464533565"
答案 0 :(得分:0)
JPQL中的日期比较可以通过以下方式完成。
如果您要与当前日期进行比较&amp;时间
select model from abc model where model.abcStartDate >= CURRENT_TIMESTAMP
and model.abcEndDate >= CURRENT_TIMESTAMP
或创建查询并传递参数
query = em.createQuery("select model from abc model where model.abcStartDate >=
:time and model.abcEndDate >= :time");
query.setParameter("time", new Date(), TemporalType.TIMESTAMP);