Skip to content

Commit b390c68

Browse files
Praful Makanichingor13
authored andcommitted
---
yaml --- r: 12213 b: refs/heads/autosynth-language c: 9529c62 h: refs/heads/master i: 12211: 53b1b87
1 parent 861b8b1 commit b390c68

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

  • branches/autosynth-language/google-cloud-clients/google-cloud-core/src

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ refs/heads/autosynth-errorreporting: 3d0566d6bebcc187f148bbed463b5a8e75b1edf6
135135
refs/heads/autosynth-firestore: 92b27fbc8855c9902168695abb0a8f1f433b750b
136136
refs/heads/autosynth-iot: 9d732be07d99843d8cb53d34ec0837328a807fce
137137
refs/heads/autosynth-kms: dcc6e15d68759010c8735cc868135bd7e6c1cc5f
138-
refs/heads/autosynth-language: f5c03944b5628d53edfcd65b504deacc6fe002a9
138+
refs/heads/autosynth-language: 9529c62031422f6b947e6d19273efd410b8ef027
139139
refs/heads/autosynth-os-login: 13200d5c38f1f09e1625c4f2dc1c3b18bfdc5857
140140
refs/heads/autosynth-redis: 1259b3a2193c6ee73b91064b5ebdedd6b51396bc
141141
refs/heads/autosynth-scheduler: 22f042c3461cee9346c46fdade5c8fad1a80f431

branches/autosynth-language/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Date.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616

1717
package com.google.cloud;
1818

19-
import com.google.api.core.BetaApi;
20-
import com.google.common.base.Preconditions;
21-
2219
import java.io.Serializable;
20+
import java.util.Calendar;
2321
import java.util.Objects;
2422
import java.util.regex.Matcher;
2523
import java.util.regex.Pattern;
2624

25+
import com.google.api.core.BetaApi;
26+
import com.google.common.base.Preconditions;
27+
2728
/**
2829
* Represents a Date without time, such as 2017-03-17. Date is timezone independent.
2930
*/
@@ -69,6 +70,40 @@ public static Date parseDate(String date) {
6970
return new Date(year, month, dayOfMonth);
7071
}
7172

73+
/**
74+
* Convert a Google Date to a Java Util Date.
75+
*
76+
* @param date the date of the Google Date.
77+
* @return java.util.Date
78+
*/
79+
public static java.util.Date toJavaUtilDate(Date date) {
80+
Calendar cal = Calendar.getInstance();
81+
cal.set(Calendar.HOUR_OF_DAY, 0);
82+
cal.set(Calendar.MINUTE, 0);
83+
cal.set(Calendar.SECOND, 0);
84+
cal.set(Calendar.MILLISECOND, 0);
85+
// Calender.MONTH starts from 0 while G C date starts from 1
86+
cal.set(date.year, date.month - 1, date.dayOfMonth);
87+
return cal.getTime();
88+
}
89+
90+
/**
91+
* Convert a Java Util Date to a Google Date.
92+
*
93+
* @param date the date of the java.util.Date
94+
* @return Google Java Date
95+
*/
96+
public static Date fromJavaUtilDate(java.util.Date date) {
97+
Calendar cal = Calendar.getInstance();
98+
cal.setTime(date);
99+
cal.set(Calendar.HOUR_OF_DAY, 0);
100+
cal.set(Calendar.MINUTE, 0);
101+
cal.set(Calendar.SECOND, 0);
102+
cal.set(Calendar.MILLISECOND, 0);
103+
// Calender.MONTH starts from 0 while G C date starts from 1
104+
return new Date(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH));
105+
}
106+
72107
/** Returns the year. */
73108
public int getYear() {
74109
return year;

branches/autosynth-language/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/DateTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@
1919
import static com.google.common.testing.SerializableTester.reserializeAndAssert;
2020
import static com.google.common.truth.Truth.assertThat;
2121

22-
import com.google.common.testing.EqualsTester;
22+
import java.text.ParseException;
23+
import java.text.SimpleDateFormat;
24+
2325
import org.junit.Test;
2426
import org.junit.runner.RunWith;
2527
import org.junit.runners.JUnit4;
2628

29+
import com.google.common.testing.EqualsTester;
30+
2731
/** Unit tests for {@link Date}. */
2832
@RunWith(JUnit4.class)
2933
public class DateTest {
3034

35+
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
36+
3137
@Test
3238
public void parseDate() {
3339
Date date = Date.parseDate("2016-09-18");
@@ -71,6 +77,23 @@ public void serialization() {
7177
reserializeAndAssert(Date.fromYearMonthDay(2017, 4, 20));
7278
}
7379

80+
@Test
81+
public void testToJavaUtilDate() throws ParseException {
82+
Date gcDate = Date.parseDate("2016-09-18");
83+
java.util.Date juDate1 = SIMPLE_DATE_FORMAT.parse("2016-09-18");
84+
java.util.Date juDate2 = Date.toJavaUtilDate(gcDate);
85+
assertThat(juDate1).isEqualTo(juDate2);
86+
}
87+
88+
@Test
89+
public void testFromJavaUtilDate() throws ParseException {
90+
java.util.Date juDate = SIMPLE_DATE_FORMAT.parse("2016-09-18");
91+
Date gcDate = Date.fromJavaUtilDate(juDate);
92+
assertThat(gcDate.getYear()).isEqualTo(2016);
93+
assertThat(gcDate.getMonth()).isEqualTo(9);
94+
assertThat(gcDate.getDayOfMonth()).isEqualTo(18);
95+
}
96+
7497
private void assertDescending(Date... dates) {
7598
for (int i = 0; i < dates.length - 1; i++) {
7699
assertThat(dates[i]).isEquivalentAccordingToCompareTo(dates[i]);

0 commit comments

Comments
 (0)