java - Validation of a date with Hibernate -


we have existing hotel management system. asked add date validation in "create accommodation" function in system. dialog looks this:

enter image description here

the "end date" validated shown in code below. @future annotation in hibernate ensures date in future.

@notnull @future @datetimeformat(pattern = "dd/mm/yyyy") @temporal(temporaltype.date) private date enddate; 

edit

i asked add validation "start date". present or future date allowed. tried use @present annotation, guess there no such thing. unfortunately, @future not accept today's date. new kind of thing. hope can me. thank you.

hibernate

you can use

@creationtimestamp @temporal(temporaltype.date) @column(name = "create_date") private date startdate; 

or on update

@updatetimestamp @temporal(temporaltype.timestamp) @column(name = "modify_date") private date startdate; 

java (jpa)

you can define field date startdate; , use

@prepersist protected void oncreatestartdate() { startdate = new date(); 

or on update

@preupdate protected void onupdatestartdate() { startdate = new date(); 

update , example

after have updated question not fix start date present, have different approach. need write custom validator check if date or in future, here.

therefore can introduce new annotation in presentorfuture.java:

@target({ elementtype.field, elementtype.method, elementtype.parameter }) @retention(retentionpolicy.runtime) @constraint(validatedby = presentorfuturevalidator.class) @documented public @interface presentorfuture {     string message() default "{presentorfuture.message}";     class<?>[] groups() default {};     class<? extends payload>[] payload() default {}; } 

then have define validator in presentorfuturevalidator.java:

public class presentorfuturevalidator     implements constraintvalidator<presentorfuture, date> {      public final void initialize(final presentorfuture annotation) {}      public final boolean isvalid(final date value,         final constraintvalidatorcontext context) {          // use date comparison         calendar calendar = calendar.getinstance();          calendar.set(calendar.hour_of_day, 0);         calendar.set(calendar.minute, 0);         calendar.set(calendar.second, 0);          date today = calendar.gettime();          // date must after today or today (== not before today)         return !value.before(today) || value.after(today);      } } 

then have set:

@notnull @presentorfuture @datetimeformat(pattern = "dd/mm/yyyy") @temporal(temporaltype.date) private date startdate; 

well, exhausive. have not tested myself, since not have set-up now, should work. hope helps.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -