Java 8 DateTime support
JSF 2.3 add alignments with Java 8, including Java 8 DateTime APIs.
The f:convertDateTime supports the following DateTime types from Java 8.
  • LocalDate
  • LocalDateTime
  • LocalTime
  • OffsetTime
  • OffsetDateTime
  • ZonedDateTime
Let's write a simple sample to try it.
Create a backing bean.
1
@ViewScoped
2
@Named("dateTimeBean")
3
public class DateTimeBean implements Serializable {
4
5
private static final Logger LOG = Logger.getLogger(DateTimeBean.class.getName());
6
7
private LocalDate localDate = LocalDate.now();
8
private LocalTime localTime = LocalTime.now();
9
private LocalDateTime localDateTime = LocalDateTime.now();
10
private OffsetTime offsetTime = OffsetTime.now();
11
private OffsetDateTime offsetDateTime = OffsetDateTime.now();
12
private ZonedDateTime zonedDateTime = ZonedDateTime.now();
13
14
public LocalDate getLocalDate() {
15
return localDate;
16
}
17
18
public void setLocalDate(LocalDate localDate) {
19
this.localDate = localDate;
20
}
21
22
public LocalTime getLocalTime() {
23
return localTime;
24
}
25
26
public void setLocalTime(LocalTime localTime) {
27
this.localTime = localTime;
28
}
29
30
public LocalDateTime getLocalDateTime() {
31
return localDateTime;
32
}
33
34
public void setLocalDateTime(LocalDateTime localDateTime) {
35
this.localDateTime = localDateTime;
36
}
37
38
public OffsetTime getOffsetTime() {
39
return offsetTime;
40
}
41
42
public void setOffsetTime(OffsetTime offsetTime) {
43
this.offsetTime = offsetTime;
44
}
45
46
public OffsetDateTime getOffsetDateTime() {
47
return offsetDateTime;
48
}
49
50
public void setOffsetDateTime(OffsetDateTime offsetDateTime) {
51
this.offsetDateTime = offsetDateTime;
52
}
53
54
public ZonedDateTime getZonedDateTime() {
55
return zonedDateTime;
56
}
57
58
public void setZonedDateTime(ZonedDateTime zonedDateTime) {
59
this.zonedDateTime = zonedDateTime;
60
}
61
62
63
}
Copied!
Create a facelets template to accept input fields and render the values.
1
<h:panelGroup layout="block" id="messageFromInputBox">
2
LocalDate is :: #{dateTimeBean.localDate} <br />
3
LocalTime is :: #{dateTimeBean.localTime} <br />
4
LocalDateTime is :: #{dateTimeBean.localDateTime} <br />
5
OffsetTime is :: #{dateTimeBean.offsetTime} <br />
6
OffsetDateTime is :: #{dateTimeBean.offsetDateTime} <br />
7
ZonedDateTime is :: #{dateTimeBean.zonedDateTime} <br />
8
</h:panelGroup>
9
<h:form id="form">
10
<div>
11
<h:outputLabel for="localDate" value="LocalDate" />
12
<h:inputText
13
id="localDate"
14
value="#{dateTimeBean.localDate}">
15
<f:convertDateTime type="localDate"/>
16
</h:inputText>
17
</div>
18
<div>
19
<h:outputLabel for="localTime" value="LocalTime" />
20
<h:inputText
21
id="localTime"
22
value="#{dateTimeBean.localTime}">
23
<f:convertDateTime type="localTime"/>
24
</h:inputText>
25
</div>
26
<div>
27
<h:outputLabel for="localDateTime" value="LocalDateTime" />
28
<h:inputText
29
id="localDateTime"
30
value="#{dateTimeBean.localDateTime}">
31
<f:convertDateTime type="localDateTime"/>
32
</h:inputText>
33
</div>
34
35
<div>
36
<h:outputLabel for="offsetTime" value="OffsetTime" />
37
<h:inputText
38
id="offsetTime"
39
value="#{dateTimeBean.offsetTime}">
40
<f:convertDateTime type="offsetTime"/>
41
</h:inputText>
42
</div>
43
44
<div>
45
<h:outputLabel for="offsetDateTime" value="OffsetDateTime" />
46
<h:inputText
47
id="offsetDateTime"
48
value="#{dateTimeBean.offsetDateTime}">
49
<f:convertDateTime type="offsetDateTime"/>
50
</h:inputText>
51
</div>
52
53
<div>
54
<h:outputLabel for="zonedDateTime" value="ZonedDateTime" />
55
<h:inputText
56
id="zonedDateTime"
57
value="#{dateTimeBean.zonedDateTime}">
58
<f:convertDateTime type="zonedDateTime"/>
59
</h:inputText>
60
</div>
61
62
<div>
63
<h:commandButton
64
id="saveDateTime"
65
value="Save DateTime">
66
<f:ajax execute="@form" render=":messageFromInputBox" />
67
</h:commandButton>
68
</div>
69
</h:form>
Copied!
Run the project in NetBeans IDE, after it is deployed successfully, open your browser and navigate to http://localhost:8080/jsf-datetime.
JSF Java 8 DateTime
Grab the source codes from my GitHub account, and have a try.
Last modified 8mo ago
Copy link