【问题标题】:The date in the Primefaces datatable does not display correctly [duplicate]Primefaces 数据表中的日期无法正确显示 [重复]
【发布时间】:2016-01-18 19:53:58
【问题描述】:

当我尝试使用 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>

支持bean等是:

@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。

提前致谢。

【问题讨论】:

    标签: java jsf jsf-2 primefaces datatable


    【解决方案1】:

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

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

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

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

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

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

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

    祝你好运, 瑞恩

    【讨论】:

    • 瑞恩,很好的答案,谢谢。我忘记了夏天的时间。没有转换器的显示器仍然与带有转换器的显示器不匹配。但我现在知道原因了。最好的问候。
    • 上次遇到这个问题时,我从数据库中获取了日期,而 JDBC 方法 getDate、getTime 和 getTimestamp 都将时区作为参数,因此我总是将这些方法显式传递给 UTC 时区。有些人也使用new Date(oldDate.toString()) 作为解决方法。一些组合会起作用。
    • 我有一个类似于代码 new Date(oldDate.toString()) 的解决方法。您明确传递方法的建议更好。再次感谢。
    猜你喜欢
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 2019-05-27
    • 2018-02-23
    相关资源
    最近更新 更多