Skip to content

Commit

Permalink
Using java.time.LocalDate instead of java.util.Date
Browse files Browse the repository at this point in the history
  • Loading branch information
arey committed Jun 20, 2018
1 parent 25d78d3 commit 03a1724
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
Expand Up @@ -28,11 +28,9 @@
import javax.persistence.OneToMany;
import javax.persistence.Table;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -49,9 +47,8 @@
public class Pet extends NamedEntity {

@Column(name = "birth_date")
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "yyyy/MM/dd")
private Date birthDate;
private LocalDate birthDate;

@ManyToOne
@JoinColumn(name = "type_id")
Expand All @@ -65,11 +62,11 @@ public class Pet extends NamedEntity {
private Set<Visit> visits;


public void setBirthDate(Date birthDate) {
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}

public Date getBirthDate() {
public LocalDate getBirthDate() {
return this.birthDate;
}

Expand Down
Expand Up @@ -23,9 +23,7 @@
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;
import java.time.LocalDate;

/**
* Simple JavaBean domain object representing a visit.
Expand All @@ -40,9 +38,8 @@ public class Visit extends BaseEntity {
* Holds value of property date.
*/
@Column(name = "visit_date")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy/MM/dd")
private Date date;
private LocalDate date;

/**
* Holds value of property description.
Expand All @@ -63,7 +60,7 @@ public class Visit extends BaseEntity {
* Creates a new instance of Visit for the current date
*/
public Visit() {
this.date = new Date();
this.date = LocalDate.now();
}


Expand All @@ -72,7 +69,7 @@ public Visit() {
*
* @return Value of property date.
*/
public Date getDate() {
public LocalDate getDate() {
return this.date;
}

Expand All @@ -81,7 +78,7 @@ public Date getDate() {
*
* @param date New value of property date.
*/
public void setDate(Date date) {
public void setDate(LocalDate date) {
this.date = date;
}

Expand Down
Expand Up @@ -17,6 +17,7 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.Date;

import org.springframework.jdbc.core.RowMapper;
Expand All @@ -32,8 +33,7 @@ public JdbcPet mapRow(ResultSet rs, int rownum) throws SQLException {
JdbcPet pet = new JdbcPet();
pet.setId(rs.getInt("pets.id"));
pet.setName(rs.getString("name"));
Date birthDate = rs.getDate("birth_date");
pet.setBirthDate(new Date(birthDate.getTime()));
pet.setBirthDate(rs.getObject("birth_date", LocalDate.class));
pet.setTypeId(rs.getInt("type_id"));
pet.setOwnerId(rs.getInt("owner_id"));
return pet;
Expand Down
Expand Up @@ -21,6 +21,7 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.Date;

/**
Expand All @@ -33,8 +34,7 @@ class JdbcVisitRowMapper implements RowMapper<Visit> {
public Visit mapRow(ResultSet rs, int row) throws SQLException {
Visit visit = new Visit();
visit.setId(rs.getInt("visit_id"));
Date visitDate = rs.getDate("visit_date");
visit.setDate(new Date(visitDate.getTime()));
visit.setDate(rs.getObject("visit_date", LocalDate.class));
visit.setDescription(rs.getString("description"));
return visit;
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp
@@ -1,6 +1,5 @@
<%@ page session="false" trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %>
Expand Down Expand Up @@ -53,7 +52,7 @@
<dt>Name</dt>
<dd><c:out value="${pet.name}"/></dd>
<dt>Birth Date</dt>
<dd><fmt:formatDate value="${pet.birthDate}" pattern="yyyy-MM-dd"/></dd>
<dd><petclinic:localDate date="${pet.birthDate}" pattern="yyyy-MM-dd"/></dd>
<dt>Type</dt>
<dd><c:out value="${pet.type.name}"/></dd>
</dl>
Expand All @@ -68,7 +67,7 @@
</thead>
<c:forEach var="visit" items="${pet.visits}">
<tr>
<td><fmt:formatDate value="${visit.date}" pattern="yyyy-MM-dd"/></td>
<td><petclinic:localDate date="${visit.date}" pattern="yyyy-MM-dd"/></td>
<td><c:out value="${visit.description}"/></td>
</tr>
</c:forEach>
Expand Down
5 changes: 2 additions & 3 deletions src/main/webapp/WEB-INF/jsp/pets/createOrUpdateVisitForm.jsp
Expand Up @@ -2,7 +2,6 @@
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %>


Expand All @@ -29,7 +28,7 @@
</thead>
<tr>
<td><c:out value="${visit.pet.name}"/></td>
<td><fmt:formatDate value="${visit.pet.birthDate}" pattern="yyyy/MM/dd"/></td>
<td><petclinic:localDate date="${visit.pet.birthDate}" pattern="yyyy/MM/dd"/></td>
<td><c:out value="${visit.pet.type.name}"/></td>
<td><c:out value="${visit.pet.owner.firstName} ${visit.pet.owner.lastName}"/></td>
</tr>
Expand Down Expand Up @@ -59,7 +58,7 @@
<c:forEach var="visit" items="${visit.pet.visits}">
<c:if test="${!visit['new']}">
<tr>
<td><fmt:formatDate value="${visit.date}" pattern="yyyy/MM/dd"/></td>
<td><petclinic:localDate date="${visit.date}" pattern="yyyy/MM/dd"/></td>
<td><c:out value="${visit.description}"/></td>
</tr>
</c:if>
Expand Down
11 changes: 11 additions & 0 deletions src/main/webapp/WEB-INF/tags/localDate.tag
@@ -0,0 +1,11 @@
<%@ tag body-content="empty" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ attribute name="date" required="true" type="java.time.LocalDate" %>
<%@ attribute name="pattern" required="true" type="java.lang.String" %>


<fmt:parseDate value="${date}" pattern="yyyy-MM-dd" var="parsedDate" type="date"/>
<fmt:formatDate value="${parsedDate}" type="date" pattern="${pattern}"/>
Expand Up @@ -17,8 +17,8 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.time.LocalDate;
import java.util.Collection;
import java.util.Date;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -135,7 +135,7 @@ public void shouldInsertPetIntoDatabaseAndGenerateId() {
pet.setName("bowser");
Collection<PetType> types = this.clinicService.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(new Date());
pet.setBirthDate(LocalDate.now());
owner6.addPet(pet);
assertThat(owner6.getPets().size()).isEqualTo(found + 1);

Expand Down

0 comments on commit 03a1724

Please sign in to comment.