Silly Question about a validation case in Java and temporary variables

余生长醉 提交于 2021-01-29 15:54:16

问题


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.

Update: More code

 * @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;

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;
    }

回答1:


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;
    }

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.



来源:https://stackoverflow.com/questions/61376617/silly-question-about-a-validation-case-in-java-and-temporary-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!