2015-01-09 35 views
4

從JSON,我得到的如何轉換日期以特定格式Freemarker模板或JavaScript

"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

我需要刪除其餘的時間格林尼治標準時間,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)" 

任何幫助表示讚賞。

謝謝

回答

6

首先,這是什麼格式?我的意思是,如果你可以影響某人使用標準格式(大多數情況下),這將有助於每個人。無論如何,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} 
+0

謝謝..這是工作'<#setting date_format = 「dd-MM-yyyy」> <#setting locale =「en_US」> <#assign createdOn = variable.createdOn> $ {createdOn?datetime(「MMM dd yyyy HH:mm:ss'GMT'Z」)) ?date} \t' – rakesh

8

你試過嗎?

"${variable.createdOn?datetime?string('dd-MM-yyyy')}" 

這裏是鏈接到文件:http://freemarker.org/docs/ref_builtins_date.html

+0

@腳氣,我已經試過this.it是給'例外:java.text.ParseException - 無法解析的日期:「2015年1月9日12:36 :18 GMT + 0530(IST)「' – rakesh

+0

@rakesh我相信你應該用單引號替換雙引號。 – Atieh

1

您可以創建自己的自定義功能和使用getDategetMonthgetFullYear方法來格式化日期。

請注意,您必須將字符串日期格式解析爲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>

+0

謝謝阿曼,但它不能解決我的問題。創建的日期可能會有所不同,如昨天,今天或一個月前。我的意思是說我需要從JSON中讀取它。對不起,如果我不清楚。 – rakesh

+0

我編輯了我的代碼。您必須從JSON中將字符串格式解析爲日期對象 – arman1991

+0

您還希望根據日期打印「昨天」,「回退」等選項? – arman1991

2
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()); 

這應該做的工作。您可以額外調整它

+0

@Bowdzone,感謝您的編輯建議,這是我的第一個回覆,並且我沒有注意到它:) –

+0

謝謝,這種方法很好用javascript – rakesh

0

我就這樣去了。我創建了一個對象格式化程序並將其傳遞給模板模型。我在模板中調用formatter.format(date)。

模板。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; 
    } 
} 
相關問題