简体   繁体   English

Playframework中的日期

[英]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 运行后,所有信息都将发布到MySql,但Date字段仍为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. 控制器中日期的调试信息如下:2011年11月7日上午12:00:00因此,我认为控制器类型和型号可能存在​​冲突。 But how? 但是如何? Please help me. 请帮我。

I'm using Play 1.2.3 / 1.2.4-RC2, 我正在使用Play 1.2.3 / 1.2.4-RC2,

It handles the date posting correctly, but I have made 2 little adjustments: 它可以正确处理日期发布,但是我做了2个小调整:

1) Add @Temporal(TemporalType.DATE) before the date declaration. 1)在日期声明之前添加@Temporal(TemporalType.DATE) 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 2)在application.conf中,我已经为所有语言环境配置了日期格式

...
# 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. 播放帖子的日期正确无误,并且主要根据当前语言环境正确验证参数(例如Long / Integer / Float / Double / BigDecimal / Date)。

Hope this helps. 希望这可以帮助。

The As annotation in the controller is here to tell Play how to convert your request param into a Date. 控制器中的As注释可以告诉Play如何将您的请求参数转换为Date。

After that you have an instance of Date and you don't have to worry about the format in the model. 之后,您将拥有一个Date实例,而不必担心模型中的格式。

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. 尝试在控制器中查看“ request.params.get(“ date”)的值是什么。它的格式不是您输入的As注释中的格式,因此Play无法将字符串转换为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 因此,您必须更改视图以发布正确的String或更改As批注以根据获得的String提供正确的格式

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

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