简体   繁体   中英

Date in Playframework

I have Model which has Date information:

package models;
import java.util.Date;
import javax.persistence.*;

import play.data.binding.As;
import play.db.jpa.*;
@Entity
public class testing extends Model {
…
public  Date date;
// I even tried public @As("yyyy-MM-dd") Date date;

…
public test1 {…date…}
}

Then I have a controller which receive user submitted date (and other information), save to database

public static void post(… @As("yyyy-MM-dd") Date date, …) {
}

Others information work well, I can save them all in database except date. After running, all information are posted to MySql but Date field was still Null

Debug information for date in controller is as follow: Nov 7, 2011 12:00:00 AM So I think that it might be conflict in type of controller and model. But how? Please help me.

I'm using Play 1.2.3 / 1.2.4-RC2,

It handles the date posting correctly, but I have made 2 little adjustments:

1) Add @Temporal(TemporalType.DATE) before the date declaration. Without it the date is a time stamp (date including time), but I think that is not the issue:

...
@Entity
public class Testing extends Model {
  ...
  @Temporal(TemporalType.DATE)
  public Date date;
  ...
}

2) In the application.conf I've configuired the date format for all my locales

...
# Date format
# ~~~~~
date.format=yyyy-MM-dd
date.format.it=dd/MM/yyyy
date.format.en=MM/dd/yyyy
date.format.fr=dd/MM/yyyy
#

Play posts dates correctly and mainly correctly validates parameters (ex. Long/Integer/Float/Double/BigDecimal/Date) based on current locale.

Hope this helps.

The As annotation in the controller is here to tell Play how to convert your request param into a Date.

After that you have an instance of Date and you don't have to worry about the format in the model.

Try to see in your controller what is the value of "request.params.get("date"). It is not in the format as you put in your As annotation then Play is not able to convert the string into Date.

I think this is your problem. So you have to change your view to post the right String or to change your As annotation to provide the right format according to the String you get

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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