2014-01-16 54 views
0

我想在jsf頁面中顯示當前的語言環境系統日期。我有下面的代碼,但它不工作。如何在JSF的網頁上顯示系統日期?

<p:column styleClass="columnA">SOP Date</p:column> 
<p:column styleClass="columnB"> 
    <h:outputText value="#{bean.currentDate}" /> 
</p:column> 

管理bean看起來像代碼:

@ManagedBean(name = "bean") 
public class SopDate implements Serializable { 

    public String currentDate; 

    @PostConstruct 
    public void init(){ 
     SopCurrentDate(); 
    } 

    public void SopCurrentDate(){ 
     DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()); 
     Calendar cal = Calendar.getInstance(); 
     this.currentDate = df.format(cal.getTime()); 
     } 
     // getter and setter 
} 
+0

什麼你的意思做'它不是working'? –

+0

我的意思是系統日期沒有使用上面的代碼顯示在網頁上。 – Rahul

+0

@PostConstruct不工作? –

回答

0

例如:

<h:outputText value="#{bean.currentDate}"> 
    <f:convertDateTime type="date" pattern="#{bean.format}" timeZone="(here your timezone)"/> 
</h:outputText> 

豆:

@ManagedBean(name = "bean") 
@ViewScoped 
public class SopDate implements Serializable { 

    public Date currentDate; 
    public String format; 

    @PostConstruct 
    public void init(){ 
     SopCurrentDate(); 
    } 

    public void SopCurrentDate(){ 
     currentDate = new Date(); 
     format = "dd-MM-yyyy"; 
    } 
     // getter and setter 
} 
+0

您不必傳遞字符串日期,就可以使用Date屬性來顯示日期。 –

+0

日期格式應該從瀏覽器設置中獲取。 – Rahul

+0

當您可以在Bean中擁有其他屬性時,我將編輯答案。 –