From c0748e3e8224ab91bb7fea41a5bc0c71158e2ce8 Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Wed, 27 Jun 2018 18:34:37 +0200 Subject: [PATCH 1/2] Use Java 8 LocalDate instead of java.util.Date See gh-328 --- .../samples/petclinic/owner/Pet.java | 11 ++++------- .../samples/petclinic/visit/Visit.java | 13 +++++-------- .../resources/templates/owners/ownerDetails.html | 6 +++--- .../templates/pets/createOrUpdateVisitForm.html | 6 +++--- .../petclinic/service/ClinicServiceTests.java | 4 ++-- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Pet.java b/src/main/java/org/springframework/samples/petclinic/owner/Pet.java index 5e226a180..795e2a94e 100755 --- a/src/main/java/org/springframework/samples/petclinic/owner/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Pet.java @@ -15,9 +15,9 @@ */ package org.springframework.samples.petclinic.owner; +import java.time.LocalDate; import java.util.ArrayList; import java.util.Collections; -import java.util.Date; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; @@ -31,8 +31,6 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; @@ -52,9 +50,8 @@ import org.springframework.samples.petclinic.visit.Visit; 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") @@ -67,11 +64,11 @@ public class Pet extends NamedEntity { @OneToMany(cascade = CascadeType.ALL, mappedBy = "petId", fetch = FetchType.EAGER) private Set visits = new LinkedHashSet<>(); - public void setBirthDate(Date birthDate) { + public void setBirthDate(LocalDate birthDate) { this.birthDate = birthDate; } - public Date getBirthDate() { + public LocalDate getBirthDate() { return this.birthDate; } diff --git a/src/main/java/org/springframework/samples/petclinic/visit/Visit.java b/src/main/java/org/springframework/samples/petclinic/visit/Visit.java index ce10d7b12..d21f60dfc 100755 --- a/src/main/java/org/springframework/samples/petclinic/visit/Visit.java +++ b/src/main/java/org/springframework/samples/petclinic/visit/Visit.java @@ -15,13 +15,11 @@ */ package org.springframework.samples.petclinic.visit; -import java.util.Date; +import java.time.LocalDate; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; import javax.validation.constraints.NotEmpty; import org.springframework.format.annotation.DateTimeFormat; @@ -38,9 +36,8 @@ import org.springframework.samples.petclinic.model.BaseEntity; public class Visit extends BaseEntity { @Column(name = "visit_date") - @Temporal(TemporalType.TIMESTAMP) @DateTimeFormat(pattern = "yyyy-MM-dd") - private Date date; + private LocalDate date; @NotEmpty @Column(name = "description") @@ -53,14 +50,14 @@ 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(); } - public Date getDate() { + public LocalDate getDate() { return this.date; } - public void setDate(Date date) { + public void setDate(LocalDate date) { this.date = date; } diff --git a/src/main/resources/templates/owners/ownerDetails.html b/src/main/resources/templates/owners/ownerDetails.html index 746d569be..ed89462b3 100644 --- a/src/main/resources/templates/owners/ownerDetails.html +++ b/src/main/resources/templates/owners/ownerDetails.html @@ -47,7 +47,7 @@
Birth Date
+ th:text="${#temporals.format(pet.birthDate, 'yyyy-MM-dd')}" />
Type
@@ -61,7 +61,7 @@ - + @@ -80,4 +80,4 @@ - \ No newline at end of file + diff --git a/src/main/resources/templates/pets/createOrUpdateVisitForm.html b/src/main/resources/templates/pets/createOrUpdateVisitForm.html index 4401d36ce..609a735f9 100644 --- a/src/main/resources/templates/pets/createOrUpdateVisitForm.html +++ b/src/main/resources/templates/pets/createOrUpdateVisitForm.html @@ -21,7 +21,7 @@ + th:text="${#temporals.format(pet.birthDate, 'yyyy-MM-dd')}" /> @@ -52,10 +52,10 @@ Description - + - \ No newline at end of file + diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java index 7ed5bf8a5..6ea694120 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java @@ -2,8 +2,8 @@ package org.springframework.samples.petclinic.service; 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.junit.runner.RunWith; @@ -140,7 +140,7 @@ public class ClinicServiceTests { pet.setName("bowser"); Collection types = this.pets.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); From 4c22d010ea92796f61be2d814ebc3c03beb9d4cf Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 9 Jul 2018 16:47:34 +0200 Subject: [PATCH 2/2] Polish "Use Java 8 LocalDate instead of java.util.Date" Closes gh-328 --- .../samples/petclinic/owner/Pet.java | 2 +- .../samples/petclinic/visit/Visit.java | 2 +- .../petclinic/service/ClinicServiceTests.java | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Pet.java b/src/main/java/org/springframework/samples/petclinic/owner/Pet.java index 795e2a94e..c225b8d8a 100755 --- a/src/main/java/org/springframework/samples/petclinic/owner/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Pet.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/springframework/samples/petclinic/visit/Visit.java b/src/main/java/org/springframework/samples/petclinic/visit/Visit.java index d21f60dfc..ab6e3319c 100755 --- a/src/main/java/org/springframework/samples/petclinic/visit/Visit.java +++ b/src/main/java/org/springframework/samples/petclinic/visit/Visit.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java index 6ea694120..9f12151d9 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2012-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.samples.petclinic.service; import static org.assertj.core.api.Assertions.assertThat;