2016-01-22 244 views
0

我想爲選定的日期製作每週範圍日期,當我點擊日曆上的某個日期時會捕獲該日期。這是我所做的代碼。每週日期範圍

package com.cc.cielo.backingbean.timesheet; 

import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import org.joda.time.DateTime; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 


import com.cc.cielo.backingbean.BaseBackingBean; 
import com.cc.cielo.managedbean.timesheet.TimesheetManagedBean; 

@ManagedBean(name = "timesheetBackingBean") 
@RequestScoped 
public class TimesheetBackingBean extends BaseBackingBean<TimesheetManagedBean> { 


public String createDateRange() { 

    Date selDate = this.getManagedBean().getSelectedDate(); 

    return null; 
} 

public TimesheetBackingBean(){ 


} 

} 

<p:column colspan="2" > 
</p:column> 
<p:column colspan="7"> 
    <p:panelGrid style="width:100%"> 
    <p:row style="color: ##bfbfbf"> 
    <p:column style="width:20%"> 
     <p:button icon="ui-icon-triangle-1-w" disabled="false"> 
     <f:param name="productId" value="10" /> 
     </p:button> 
    </p:column> 
    <p:column style="width:60%; text-align:center"> 
     <h:outputLabel value="#{timesheetBackingBean.createDateRange}" /> 
    </p:column> 
    <p:column style="width:20%; text-align:right"> 
     <p:button icon="ui-icon-triangle-1-e" disabled="false"> 
     <f:param name="productId1" value="10" /> 
     </p:button> 
    </p:column> 
    </p:row> 
    </p:panelGrid> 
</p:column> 



<p:column colspan="7" style="text-align: right;"> 
<p:calendar id="event" value="#{timesheetBackingBean.managedBean.selectedDate}" showOn="button" pattern="dd/MM/yyyy" mask="true"> 
    <p:ajax event="dateSelect" listener="#{timesheetBackingBean.createDateRange}" /> 
</p:calendar> 
</p:column> 
</p:row> 
     </f:facet> 

現在我想粘貼每週範圍從我所選用成輸出標籤的日期。

+0

粘貼它?像在ctrl-v? – Kukeltje

回答

0

隨着谷歌的一些研究。我找到了我的問題的答案。

public String createDateRange() { 

     Date selDate = this.getManagedBean().getSelectedDate();  
     Date date = new Date(); 
     Calendar c = new GregorianCalendar(); 
     c.setTime(selDate); 
     c.setFirstDayOfWeek(Calendar.MONDAY); 
     int i = c.get(Calendar.DAY_OF_WEEK) - c.getFirstDayOfWeek(); 
     c.add(Calendar.DATE, -i); 
     Date start = c.getTime(); 
     c.add(Calendar.DATE, 6); 
     Date end = c.getTime(); 
     System.out.println(start + " - " + end); 
     SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy"); 
     String formatted = format1.format(start); 
     String formatted2 = format1.format(end); 

     this.getManagedBean().setDateRange(formatted + " - " + formatted2); 

     return null; 
    } 


    <p:column style="width:60%;text-align: center;"> 
     <h:outputLabel id="dateRange" value="#{timesheetBackingBean.managedBean.dateRange}"/> 
    </p:column> 
    <p:column style="width:20%; text-align:right"> 
     <p:button icon="ui-icon-triangle-1-e" disabled="false"> 
     <f:param name="productId1" value="10" /> 
     </p:button> 
    </p:column> 
    </p:row> 
    </p:panelGrid> 
</p:column> 



<p:column colspan="7" style="text-align: right;"> 
<p:calendar id="event" value="#{timesheetBackingBean.managedBean.selectedDate}" imediate="true" showOn="button" pattern="dd/MM/yyyy" mask="true"> 
    <p:ajax event="dateSelect" listener="#{timesheetBackingBean.createDateRange}" update="dateRange" /> 
</p:calendar> 
</p:column> 
+1

請在答案中添加一些解釋並將代碼格式化一點(拆分xhtml和java)。而且,由於「粘貼」這個問題,這個問題也得到了改進,在這種情況下這是一個完全怪異的術語 – Kukeltje