关于Java和临时变量中的验证案例的愚蠢问题 [英] Silly Question about a validation case in Java and temporary variables

查看:60
本文介绍了关于Java和临时变量中的验证案例的愚蠢问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在更新日期之前设置日期的临时变量?我想比较一下我的初始日期,然后再进行更新.

How does one set a temporary variable of date before it is updated? I would like to compare my initial date before it is updated.

更新:更多代码

 * @hibernate.class table="t3sstations"
 * @hibernate.join name="rgnjoin" table="stnregions" optional="true"
 * @hibernate.join-key column="idstn"
 */
public class Station extends BaseSiteChildCFKObject implements Convertible, Comparable<Station> {
    private static final long serialVersionUID = -2056063261398467275L;
    protected final Log log = LogFactory.getLog(getClass());

    private Project stationProject;
    private Date project_startdate;
    private Date date_initial;

它使用休眠从SQL获取的表.它从列project_startdate中获取项目的日期.我想将最初的日期与新的/更新的日期进行比较.它在整个类中更新相同的值.

Table it grabs from SQL using hibernate. It grabs the project's date from the column project_startdate. I want to compare the date that it is initially with the new/updated date. It updates the same value throughout the class.

    /**
     * @return
     * @hibernate.many-to-one column="idproject" not-null="false"
     * class="org.unavco.pbo.mdm.model.Project"
     */
    @Required
    public Project getStationProject() {
        return stationProject;
    }

    public void setStationProject(Project stationProject) {
        this.stationProject = stationProject;
    }


    /**
     * @return
     * @hibernate.property column="project_startdate"
     */

    public Date getProject_startdate() {
        return project_startdate;
    }


    public void setProject_startdate(Date project_startdate) {
            this.project_startdate = project_startdate;
    }

@AssertTrue
    private boolean validateProjectDate() {

        project_initialdate = new Date(project_startdate);

        if (project_startdate.before(this.project_initialdate)){
            return false;
        }

        if (project_startdate.before(this.stationProject.getStart_date()))  {

            return false;
        }

        if (this.stationProject.getEnd_date() != null) {
            if (this.project_startdate.after(this.stationProject.getEnd_date())) {
                return false;
            }
        }
        return true;
    }

推荐答案

所以我不认为我找到了一个临时值持有者,但我确实将初始日期定义为初始化程序中的另一个变量.通过将其放在此处,可以保存日期,然后通过休眠更改和更新日期.

So I don't think I found a temporary value holder but I did define the initial date as another variable inside the initializer. By placing it here, I save the date before it is changed and updated via hibernate.

public Station() {
        super();
        Date date_initial = this.project_startdate;
    }

在称为Station的类的顶部.从那里我还定义了日期初始的getter和setter.

At the top of the class called Station. From there I also defined the getter and setter for date initial.

public Date getDate_initial(){
        return date_initial;
    }

    public void setDate_initial(Date date_initial) {
        this.date_initial = date_initial;
    }

从那里,它可以准确地将初始日期与更新日期进行比较.如果安全,它将继续保存.否则,它将向用户显示错误消息.

From there it accurately compares the initial date to the updated date. If it is safe then it continues to save. Otherwise it displays an error message to the user.

这篇关于关于Java和临时变量中的验证案例的愚蠢问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆