Primefaces 数据表中的日期显示不正确
The date in the Primefaces datatable does not display correctly
当我尝试使用 Primefaces 数据表显示日期时,其中一些日期显示早了一天。
This is what it should be (shown by the Netbeans debug) and 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 中的日期应在后台使用 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" />
以便您可以看到错误日期的时间值。
祝你好运,
瑞安
当我尝试使用 Primefaces 数据表显示日期时,其中一些日期显示早了一天。 This is what it should be (shown by the Netbeans debug) and 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 中的日期应在后台使用 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" />
以便您可以看到错误日期的时间值。
祝你好运, 瑞安