Primefaces数据表中的日期无法正确显示

时间:2015-10-20 08:38:38

标签: java jsf jsf-2 primefaces datatable

当我尝试使用Primefaces数据表显示日期时,其中一些会在一天前显示。 This is what it should be (shown by the Netbeans debug)this is what displayed by Firefox.

JSF页面是:

    <h:body>
    <h:form>
        <p:outputLabel styleClass="header-calendar">From Date: </p:outputLabel>
        <p:calendar id="fromDate" value="#{datedCarFilterView.fromDate}" pattern="dd MM yyyy"  readonlyInput="true" maxdate="#{datedCarFilterView.currentDate}">
            <p:ajax event="dateSelect" update="viewDataTable" onstart="PF('vtWidget').clearFilters()"/>
        </p:calendar><br/>   
        <p:dataTable var="car" value="#{datedCarFilterView.cars}" id="viewDataTable" widgetVar="vtWidget">

            <p:column headerText="ID">
                <h:outputText value="#{car.id}" />
            </p:column>
            <p:column filterBy="#{car.color}" headerText="Color" filterMatchMode="contains">
                <h:outputText value="#{car.color}" />
            </p:column>    
            <p:column headerText="Date">
                <h:outputText value="#{car.date}">
                    <f:convertDateTime pattern="dd-MMM-yyyy" />
                </h:outputText>
            </p:column>                  
        </p:dataTable>
    </h:form>
</h:body>

支持豆等是:

@ManagedBean(name = "datedCarFilterView") @ViewScoped public class DatedCarFilterView implements Serializable {

private static final long serialVersionUID = 978770613134439198L;

private Date fromDate;

private List<DatedCar> allCars;
private final List<DatedCar> cars = new ArrayList<>();

@ManagedProperty("#{datedCarService}")
private DatedCarService service;

@PostConstruct
public void init() {
    allCars = service.createCars(100);
    fromDate = new Date(getCurrentDate().getTime() - 1000 * 3600 * 24 * 3);
    refreshCarList();
}

public void setService(DatedCarService service) {
    this.service = service;
}

public List<DatedCar> getCars() {
    return cars;
}

public Date getFromDate() {
    return fromDate;
}

public void setFromDate(Date fromDate) {
    this.fromDate = fromDate;
    refreshCarList();
}

private void refreshCarList() {
    cars.clear();
    for (DatedCar car : allCars) {
        if (car.getDate().getTime() > fromDate.getTime()) {
            cars.add(car);
        }
    }
}

public Date getCurrentDate() {
    return new Date();
}}


@ManagedBean(name = "datedCarService") @ApplicationScoped public class DatedCarService implements Serializable {

private static final long serialVersionUID = 787505400128748931L;

private final static String[] colors;

static {
    colors = new String[10];
    colors[0] = "Black";
    colors[1] = "White";
    colors[2] = "Green";
    colors[3] = "Red";
    colors[4] = "Blue";
    colors[5] = "Orange";
    colors[6] = "Silver";
    colors[7] = "Yellow";
    colors[8] = "Brown";
    colors[9] = "Maroon";
}


  public List<DatedCar> createCars(int size) {
    List<DatedCar> list = new ArrayList<>();
    for(int i = 0 ; i < size ; i++) {
        list.add(new DatedCar(getRandomId(), getRandomDate(), getRandomColor()));
    }

    return list;
}

private String getRandomId() {
    return UUID.randomUUID().toString().substring(0, 8);
}

private Date getRandomDate() {

    long time = new Date().getTime();

    int hours = (int) (Math.random() * 360); 

    return new Date( time - hours*3600*1000);
}  

private String getRandomColor() {
    return colors[(int) (Math.random() * 10)];
}    

public List<String> getColors() {
    return Arrays.asList(colors);
} }   


public class DatedCar implements Serializable {

private static final long serialVersionUID = 157675587142381235L;    

private String id;
private Date date;
private String color;

public DatedCar() {
}

public DatedCar(String id, Date date, String color) {
    this.id = id;
    this.date = date;
    this.color = color;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

@Override
public String toString() {
    return "DatedCar{" + "id=" + id + '}';
}

@Override
public int hashCode() {
    int hash = 7;
    hash = 41 * hash + Objects.hashCode(this.id);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final DatedCar other = (DatedCar) obj;
    return Objects.equals(this.id, other.id);
} }

我正在使用JSF 2.2,Primefaces 5.2和Glassfish 4.1。

提前致谢。

1 个答案:

答案 0 :(得分:2)

这看起来像是时区问题 Java中的日期应该使用UTC时间并将其转换为由本地计算机确定的适当时区。

您的日期类似于2015年10月18日00:23 + 0100,您可能会显示第17个,因为它将日期转换为UTC(或者它的日期)将UTC转换为-0100或其他类似组合)。这似乎是将日期推迟一天:17-OCT-2015 23:23。

尝试打印日期,执行以下行,再次创建日期对象,然后重新打印。

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

您可能想要做的另一件事是在html中删除日期格式。即删除此行

<f:convertDateTime pattern="dd-MMM-yyyy" />

这样您就可以看到错误日期的时间值。

祝你好运, 莱恩