如何使用Play控制网址中日期的格式? [英] How do I control the formatting of dates in a URL with Play?

查看:147
本文介绍了如何使用Play控制网址中日期的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Play应用程序,应该在给定的日期间隔内列出购买。如果用户没有给出任何日期间隔,则应该默认在过去一年内显示购买。这是通过我的控制器中的这两种方法完成的:

I have a Play application that should list out purchases within a given date interval. If the user does not give any date interval, it should default to showing purchases within the last year. This is done with these two methods in my controller:

public static void show(String id) {
    Date today = new Date();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(today);
    calendar.add(Calendar.YEAR, -1);
    Date oneYearAgo = calendar.getTime();

    showWithInterval(id, oneYearAgo, today);
}

public static void showWithInterval(String id, Date fromDate, Date toDate) {
    List<Purchase> purchases= Purchase.find(id, fromDate, toDate);

    render(purchases, fromDate, toDate);
}

但是,这会产生一个如下所示的URL: http:// localhost:9000 / purchases / showwithinterval / 10076430719?fromDate = ISO8601:2010-01-17T19:41:20%2B0100& toDate = ISO8601:2011-01-17T19:41:20%2B0100

However, this produces a url looking like this: http://localhost:9000/purchases/showwithinterval/10076430719?fromDate=ISO8601:2010-01-17T19:41:20%2B0100&toDate=ISO8601:2011-01-17T19:41:20%2B0100

这不符合我在 date.format 中指定的日期格式c $ c> application.conf 。这种格式根本不可用,因为我想要打印日期(使用 $ {params.fromDate} ),让我的用户编辑它们以显示其他间隔。我不能在视图中格式化它们,因为它们是字符串。

This does not adhere to the date format I have specified with the date.format property in application.conf. This format is simply not usable, as I want to be able to print the dates (using ${params.fromDate}) and let my users edit them to show other intervals. I cannot format them in the view, since they are strings.

有没有人知道如何解决这个问题?

Does anyone know how to fix this?

修改:

推荐答案

将fromDate et toDate添加到您的渲染方法中:

Add fromDate et toDate to your render method :

render(purchases,fromDate,toDate);

并格式化:

${fromDate.format()}

Play将格式化日期您的application.conf中的格式配置

Play will format the date with your format configuration in application.conf

这篇关于如何使用Play控制网址中日期的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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