Skip to content

Commit 4cdaf68

Browse files
Adding Timestamp.toDate().
This will be used by consumers of the Firestore API.
1 parent 86be7e5 commit 4cdaf68

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ public static Timestamp of(Date date) {
9595
return ofTimeMicroseconds(TimeUnit.MILLISECONDS.toMicros(date.getTime()));
9696
}
9797

98+
/**
99+
* Returns a new {@code java.util.Date} corresponding to this {@code timestamp}. Any
100+
* sub-millisecond precision will be stripped.
101+
*
102+
* @return An approximate {@code java.util.Date} representation of this {@code timestamp}.
103+
*/
104+
public Date toDate() {
105+
long secondsInMilliseconds = TimeUnit.SECONDS.toMillis(this.seconds);
106+
long nanosInMilliseconds = TimeUnit.NANOSECONDS.toMillis(this.nanos);
107+
return new Date(secondsInMilliseconds + nanosInMilliseconds);
108+
}
109+
98110
/** Creates an instance with current time. */
99111
public static Timestamp now() {
100112
java.sql.Timestamp date = new java.sql.Timestamp(System.currentTimeMillis());

google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/TimestampTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
import com.google.common.testing.EqualsTester;
2323
import java.util.Calendar;
24+
import java.util.Date;
2425
import java.util.GregorianCalendar;
2526
import java.util.TimeZone;
27+
import java.util.concurrent.TimeUnit;
2628
import org.junit.Rule;
2729
import org.junit.Test;
2830
import org.junit.rules.ExpectedException;
@@ -35,6 +37,9 @@ public class TimestampTest {
3537
private static final String TEST_TIME_ISO = "2015-10-12T15:14:54Z";
3638
private static final long TEST_TIME_SECONDS = 1444662894L;
3739
private static final long TEST_TIME_MICROSECONDS = 10000100L;
40+
private static final long TEST_TIME_MILLISECONDS =
41+
TimeUnit.SECONDS.toMillis(1444662894L) + TimeUnit.MICROSECONDS.toMillis(1234);
42+
private static final Date TEST_DATE = new Date(TEST_TIME_MILLISECONDS);
3843

3944
@Rule public ExpectedException expectedException = ExpectedException.none();
4045

@@ -64,6 +69,24 @@ public void ofMicroseconds() {
6469
assertThat(timestamp.getNanos()).isEqualTo(TEST_TIME_MICROSECONDS % 1000000L * 1000);
6570
}
6671

72+
@Test
73+
public void ofDate() {
74+
Timestamp timestamp = Timestamp.of(TEST_DATE);
75+
Long expectedSeconds = TimeUnit.MILLISECONDS.toSeconds(TEST_TIME_MILLISECONDS);
76+
Long expectedNanos =
77+
TimeUnit.MILLISECONDS.toNanos(TEST_TIME_MILLISECONDS)
78+
- TimeUnit.SECONDS.toNanos(expectedSeconds);
79+
assertThat(timestamp.getSeconds()).isEqualTo(expectedSeconds);
80+
assertThat(timestamp.getNanos()).isEqualTo(expectedNanos);
81+
}
82+
83+
@Test
84+
public void toDate() {
85+
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 1234 * 1000);
86+
Date date = timestamp.toDate();
87+
assertThat(TEST_TIME_MILLISECONDS).isEqualTo(date.getTime());
88+
}
89+
6790
@Test
6891
public void toFromSqlTimestamp() {
6992
long seconds = TEST_TIME_SECONDS;

0 commit comments

Comments
 (0)