每周日期范围

时间:2016-01-22 07:09:38

标签: jsf-2 primefaces

我想为我在日历上点击日期时捕获的所选日期制作每周范围日期。这是我做的代码。

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> 

现在我想将每周范围从我选择的日期粘贴到输出标签。

1 个答案:

答案 0 :(得分: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>