简体   繁体   English

如何在Play Framework中使用Ebean比较日期?

[英]How to compare dates using Ebean in Play Framework?

Scenario.java : Scenario.java

package models;

import java.util.*;

import javax.persistence.*;

import play.db.ebean.*;

@Entity
public class Scenario extends Model {

    @Id
    public Long id;
    public String name;
    public boolean isPublic;
    public boolean isAccepted;
    public Date expirationDate;
    @ManyToOne
    public User owner;
    @ManyToMany
    public List<User> members = new ArrayList<User>(); 
    @OneToMany(cascade = CascadeType.ALL)
    public List<Checkpoint> checkpoints = new ArrayList<Checkpoint>();

    public static Model.Finder<Long, Scenario> find =
            new Finder<Long, Scenario>(Long.class, Scenario.class);

    public Scenario(String name, boolean isPublic, Date expirationDate, User owner) {
        this.name = name;
        this.isPublic = isPublic;
        this.expirationDate = expirationDate;
        this.owner = owner;
        this.isAccepted = false;
        this.members.add(owner);
    }

    public static Scenario create(String name, boolean isPublic, Date expirationDate,
            String owner) {
        Scenario scenario = new Scenario(name, isPublic, expirationDate, User.find.ref(owner));
        scenario.save();
        scenario.saveManyToManyAssociations("members");
        return scenario;
    }

    public static List<Scenario> findNotExpired(Date date) {
        return find.where().or(
                com.avaje.ebean.Expr.lt("expirationDate", date),
                com.avaje.ebean.Expr.isNull("expirationDate")
                ).findList();
    }
}

The test: 考试:

@Test
    public void findScenariosNotExpired() {
        new User("bob@gmail.com", "Bob", "secret").save();
        new User("jane@gmail.com", "Jane", "secret").save();
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); 
        Scenario.create("Scenario 1", false, null, "bob@gmail.com");
        try {
            Scenario.create("Scenario 2", false, dt.parse("2013-12-12 00:00:00"), "jane@gmail.com");
            Scenario.create("Scenario 3", false, dt.parse("2013-10-10 00:00:00"), "jane@gmail.com");
        } catch (ParseException e) {
            System.err.println("Problem with date parsing");
            e.printStackTrace();
        }

        List<Scenario> results = Scenario.findNotExpired(new Date());
        assertEquals(2, results.size());
    }

The test's result: 测试结果如下:

[error] Test models.ModelsTest.findScenariosNotExpired failed: expected:<2> but was:<3>

In the test I pass new Date() as it should include today's date (Oct 13, 2013). 在测试中我通过了new Date()因为它应包括今天的日期(2013年10月13日)。 That's why I expect the size of 2 (one null and one date before today). 这就是为什么我希望大小为2(一个空和一个日期在今天之前)。

What should I change? 我应该改变什么? I guess the problem could be with com.avaje.ebean.Expr.lt("expirationDate", date) - is it OK to compare date like this? 我想问题可能出在com.avaje.ebean.Expr.lt("expirationDate", date) - 可以比较这样的日期吗?

You need to use MM instead of mm . 您需要使用MM而不是mm

Using 运用

SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");

dt.parse("2013-12-12 00:00:00") evaluates to Sat Jan 12 00:00:00 EST 2013

dt.parse("2013-10-10 00:00:00") evaluates to Thu Jan 10 00:00:00 EST 2013

Instead use, 而是使用,

SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 

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

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