diff --git a/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Date.java b/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Date.java index 044a7ca72bf6..e70ca8e5a427 100644 --- a/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Date.java +++ b/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Date.java @@ -24,6 +24,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.threeten.bp.LocalDate; + /** * Represents a Date without time, such as 2017-03-17. Date is timezone independent. */ @@ -68,7 +70,39 @@ public static Date parseDate(String date) { int dayOfMonth = Integer.parseInt(matcher.group(3)); return new Date(year, month, dayOfMonth); } + + /** + * @param date + * @return LocalDate + */ + public static LocalDate toDate(Date date){ + return LocalDate.of(date.year, date.month, date.dayOfMonth); + } + + /** + * @param date + * @return Date + */ + public static Date toDate(LocalDate date){ + return new Date(date.getDayOfYear(), date.getMonthValue(), date.getDayOfMonth()); + } + /** + * @param date + * @return LocalDate + */ + public static LocalDate fromDate(Date date){ + return LocalDate.of(date.year, date.month, date.dayOfMonth); + } + + /** + * @param date + * @return Date + */ + public static Date fromDate(LocalDate date){ + return new Date(date.getDayOfYear(), date.getMonthValue(), date.getDayOfMonth()); + } + /** Returns the year. */ public int getYear() { return year; diff --git a/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/DateTest.java b/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/DateTest.java index 3c2817a92c68..e11e4569402a 100644 --- a/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/DateTest.java +++ b/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/DateTest.java @@ -23,6 +23,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import org.threeten.bp.LocalDate; /** Unit tests for {@link Date}. */ @RunWith(JUnit4.class) @@ -70,6 +71,40 @@ public void validOrdering() { public void serialization() { reserializeAndAssert(Date.fromYearMonthDay(2017, 4, 20)); } + + @Test + public void toLocalDate(){ + LocalDate localDate = Date.toDate(Date.parseDate("2016-09-18")); + assertThat(localDate.getYear()).isEqualTo(2016); + assertThat(localDate.getMonth().getValue()).isEqualTo(9); + assertThat(localDate.getDayOfMonth()).isEqualTo(18); + } + + @Test + public void toDate(){ + Date date = Date.toDate(LocalDate.of(2016, 9, 18)); + assertThat(date.getYear()).isEqualTo(2016); + assertThat(date.getMonth()).isEqualTo(9); + assertThat(date.getDayOfMonth()).isEqualTo(18); + + } + + @Test + public void fromLocalDate(){ + LocalDate localDate = Date.fromDate(Date.parseDate("2016-09-18")); + assertThat(localDate.getYear()).isEqualTo(2016); + assertThat(localDate.getMonth().getValue()).isEqualTo(9); + assertThat(localDate.getDayOfMonth()).isEqualTo(18); + } + + @Test + public void fromDate(){ + Date date = Date.fromDate(LocalDate.of(2016, 9, 18)); + assertThat(date.getYear()).isEqualTo(2016); + assertThat(date.getMonth()).isEqualTo(9); + assertThat(date.getDayOfMonth()).isEqualTo(18); + } + private void assertDescending(Date... dates) { for (int i = 0; i < dates.length - 1; i++) {