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..002ef84cdfa9 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 @@ -16,14 +16,15 @@ package com.google.cloud; -import com.google.api.core.BetaApi; -import com.google.common.base.Preconditions; - import java.io.Serializable; +import java.util.Calendar; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.google.api.core.BetaApi; +import com.google.common.base.Preconditions; + /** * Represents a Date without time, such as 2017-03-17. Date is timezone independent. */ @@ -69,6 +70,40 @@ public static Date parseDate(String date) { return new Date(year, month, dayOfMonth); } + /** + * Convert a Google Date to a Java Util Date. + * + * @param date the date of the Google Date. + * @return java.util.Date + */ + public static java.util.Date toJavaUtilDate(Date date) { + Calendar cal = Calendar.getInstance(); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + // Calender.MONTH starts from 0 while G C date starts from 1 + cal.set(date.year, date.month - 1, date.dayOfMonth); + return cal.getTime(); + } + + /** + * Convert a Java Util Date to a Google Date. + * + * @param date the date of the java.util.Date + * @return Google Java Date + */ + public static Date fromJavaUtilDate(java.util.Date date) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + // Calender.MONTH starts from 0 while G C date starts from 1 + return new Date(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH)); + } + /** 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..31dbf6416bbc 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 @@ -19,15 +19,21 @@ import static com.google.common.testing.SerializableTester.reserializeAndAssert; import static com.google.common.truth.Truth.assertThat; -import com.google.common.testing.EqualsTester; +import java.text.ParseException; +import java.text.SimpleDateFormat; + import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import com.google.common.testing.EqualsTester; + /** Unit tests for {@link Date}. */ @RunWith(JUnit4.class) public class DateTest { + private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); + @Test public void parseDate() { Date date = Date.parseDate("2016-09-18"); @@ -71,6 +77,23 @@ public void serialization() { reserializeAndAssert(Date.fromYearMonthDay(2017, 4, 20)); } + @Test + public void testToJavaUtilDate() throws ParseException { + Date gcDate = Date.parseDate("2016-09-18"); + java.util.Date juDate1 = SIMPLE_DATE_FORMAT.parse("2016-09-18"); + java.util.Date juDate2 = Date.toJavaUtilDate(gcDate); + assertThat(juDate1).isEqualTo(juDate2); + } + + @Test + public void testFromJavaUtilDate() throws ParseException { + java.util.Date juDate = SIMPLE_DATE_FORMAT.parse("2016-09-18"); + Date gcDate = Date.fromJavaUtilDate(juDate); + assertThat(gcDate.getYear()).isEqualTo(2016); + assertThat(gcDate.getMonth()).isEqualTo(9); + assertThat(gcDate.getDayOfMonth()).isEqualTo(18); + } + private void assertDescending(Date... dates) { for (int i = 0; i < dates.length - 1; i++) { assertThat(dates[i]).isEquivalentAccordingToCompareTo(dates[i]);