[英]How to convert date in specific format in Freemarker template or javascript
从json,我得到的价值为
"createdOn": "Jan 08 2015 20:40:56 GMT+0530 (IST)",
我在 FTL 中访问
<#list variables as variable>
<div class="reply">
${variable.createdOn}
</div>
</#list>
我得到的结果是
Jan 09 2015 12:36:18 GMT+0530 (IST)
我的首选格式是09-01-2015
我需要删除 GMT、IST 等其他时间。
如何在 Freemarker 模板或 javascript 中转换它。
更新
我试着像这样通过下面
${variable.createdOn?datetime?string("dd-MM-yyyy")}
但它给出了错误
Exception: java.text.ParseException - Unparseable date: "Jan 09 2015 12:36:18 GMT+0530 (IST)"
任何帮助表示赞赏。
谢谢
首先,那是什么格式? 我的意思是,如果你可以影响某人使用标准格式(主要是ISO),这将有助于每个人。 无论如何,FreeMarker不是一个日期解析器库,但实际上你可以这样做:
<#-- Settings you need -->
<#setting date_format="dd-MM-yyyy">
<#setting locale="en_US">
<#-- The string that comes from somewhere: -->
<#assign createdOn = 'Jan 08 2015 20:40:56 GMT+0530 (IST)'>
<#--
1. Tell FreeMarker to convert string to real date-time value
2. Convert date-time value to date-only value
3. Let FreeMarker format it according the date_format setting
-->
${createdOn?datetime("MMM dd yyyy HH:mm:ss 'GMT'Z")?date}
你试过这个吗?
"${variable.createdOn?datetime?string('dd-MM-yyyy')}"
以下是文档链接: http : //freemarker.org/docs/ref_builtins_date.html
function convertDate( date ){
dateSplit = date.toString().split( ' ' );
dateSplit[1] = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1).toString() : date.getMonth() + 1;
return dateSplit[2] + '-' + dateSplit[1] + '-' + dateSplit[3];
}
convertDate(new Date());
这应该做的工作。 你可以另外调整它
您可以创建自己的自定义函数,并使用getDate
, getMonth
和getFullYear
方法格式化日期。
请注意,您必须将字符串日期格式解析为Date对象。
<!DOCTYPE html> <html> <body> <p>Click the button to display todays day of the month in dd-MM-yyyy format.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var d = new Date("Jan 08 2015 20:40:56 GMT+0530 (IST)"); //parsing your string date format into Date object. var z = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear(); document.getElementById("demo").innerHTML = z; } </script> </body> </html>
我走了这条路。 我创建了一个对象 - 格式化程序并将其传递给模板模型。 我在模板中调用formatter.format(date)。
template.ftl
<div class="historyOrderItem">
<div>
<div>Created <#if order.created??>${formatter.format(order.created)}</#if></div>
<div>Amount ${order.amount!}</div>
<div>Currency ${order.currency!}</div>
</div>
OrderPresenter.java
@Component
public class OrderPresenter {
private static final String FORMATTER_PARAM = "formatter";
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final DateTimeFormatter FORMATTER =
DateTimeFormatter.ofPattern(DATE_TIME_FORMAT).withZone(ZoneId.systemDefault());
private Configuration configuration = prepareConfiguration();
public String toHtmlPresentation(ClientDetails clientDetails) {
try {
Template template = configuration.getTemplate(CLIENT_DATA_TEMPLATE);
Writer out = new StringWriter();
template.process(toMap(clientDetails), out);
return out.toString();
} catch (IOException | TemplateException e) {
throw new RuntimeException(e);
}
}
private Configuration prepareConfiguration() {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setDefaultEncoding(ENCODING);
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
configuration.setLogTemplateExceptions(NOT_TO_LOG_EXCEPTIONS);
configuration.setClassForTemplateLoading(OrderPresenter.class, TEMPLATES_FOLDER);
return configuration;
}
private Map<String, Object> toMap(ClientDetails clientDetails) {
Map<String, Object> res = new HashMap<>();
res.put(CLIENT_DETAILS_PARAM, clientDetails);
res.put(FORMATTER_PARAM, FORMATTER);
return res;
}
}
模板.ftl
<#list listaCategoria as c>
<tr>
<td> ${c.dataCadastro?datetime("yyyy-MM-dd")?string("dd/MM/yyyy")}</td>
</tr>
</#list>
RelatorioService.java
LocalDate dataCadastro = LocalDate.of(1995, 12, 7);
结果:
07/12/1995
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.