Skip to content

Commit fc8438b

Browse files
committed
Removing non-deprecated uses of joda time.
This works towards #3482
1 parent 821bf93 commit fc8438b

11 files changed

Lines changed: 72 additions & 39 deletions

File tree

google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryParameterValue.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
import com.google.common.collect.ImmutableList;
2323
import com.google.common.collect.Lists;
2424
import com.google.common.io.BaseEncoding;
25+
import org.threeten.bp.Instant;
26+
import org.threeten.bp.ZoneOffset;
27+
import org.threeten.bp.format.DateTimeFormatter;
28+
import org.threeten.bp.format.DateTimeParseException;
29+
2530
import java.io.Serializable;
2631
import java.math.BigDecimal;
2732
import java.util.ArrayList;
2833
import java.util.List;
2934
import javax.annotation.Nullable;
30-
import org.joda.time.DateTimeZone;
31-
import org.joda.time.format.DateTimeFormat;
32-
import org.joda.time.format.DateTimeFormatter;
3335

3436
/**
3537
* A value for a QueryParameter along with its type.
@@ -60,12 +62,12 @@
6062
public abstract class QueryParameterValue implements Serializable {
6163

6264
private static final DateTimeFormatter timestampFormatter =
63-
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSSSSZZ").withZone(DateTimeZone.UTC);
64-
private static final DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
65+
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx").withZone(ZoneOffset.UTC);
66+
private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
6567
private static final DateTimeFormatter timeFormatter =
66-
DateTimeFormat.forPattern("HH:mm:ss.SSSSSS");
68+
DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSS");
6769
private static final DateTimeFormatter datetimeFormatter =
68-
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
70+
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
6971

7072
static final Function<
7173
QueryParameterValue, com.google.api.services.bigquery.model.QueryParameterValue>
@@ -297,31 +299,31 @@ private static <T> String valueToStringOrNull(T value, StandardSQLTypeName type)
297299
throw new IllegalArgumentException("Cannot convert ARRAY to String value");
298300
case TIMESTAMP:
299301
if (value instanceof Long) {
300-
return timestampFormatter.print(((Long) value) / 1000);
302+
return timestampFormatter.format(Instant.ofEpochMilli(((Long) value) / 1000));
301303
} else if (value instanceof String) {
302304
// verify that the String is in the right format
303-
timestampFormatter.parseMillis((String) value);
305+
checkFormat(value, timestampFormatter);
304306
return (String) value;
305307
}
306308
break;
307309
case DATE:
308310
if (value instanceof String) {
309311
// verify that the String is in the right format
310-
dateFormatter.parseMillis((String) value);
312+
checkFormat(value, dateFormatter);
311313
return (String) value;
312314
}
313315
break;
314316
case TIME:
315317
if (value instanceof String) {
316318
// verify that the String is in the right format
317-
timeFormatter.parseMillis((String) value);
319+
checkFormat(value, timeFormatter);
318320
return (String) value;
319321
}
320322
break;
321323
case DATETIME:
322324
if (value instanceof String) {
323325
// verify that the String is in the right format
324-
datetimeFormatter.parseMillis((String) value);
326+
checkFormat(value, datetimeFormatter);
325327
return (String) value;
326328
}
327329
break;
@@ -332,6 +334,14 @@ private static <T> String valueToStringOrNull(T value, StandardSQLTypeName type)
332334
"Type " + type + " incompatible with " + value.getClass().getCanonicalName());
333335
}
334336

337+
private static void checkFormat(Object value, DateTimeFormatter formatter) {
338+
try {
339+
formatter.parse((String) value);
340+
} catch (DateTimeParseException e) {
341+
throw new IllegalArgumentException(e.getMessage(), e);
342+
}
343+
}
344+
335345
/** Returns a builder for a QueryParameterValue object with given value. */
336346
public abstract Builder toBuilder();
337347

google-cloud-clients/google-cloud-compute/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
<artifactId>google-api-services-compute</artifactId>
3232
<scope>compile</scope>
3333
</dependency>
34+
<dependency>
35+
<groupId>joda-time</groupId>
36+
<artifactId>joda-time</artifactId>
37+
<scope>compile</scope>
38+
</dependency>
3439

3540
<!-- Test dependencies -->
3641
<dependency>

google-cloud-clients/google-cloud-contrib/google-cloud-nio/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
<pattern>org</pattern>
113113
<shadedPattern>shaded.cloud_nio.org</shadedPattern>
114114
<includes>
115-
<include>org.joda.**</include>
116115
<include>org.apache.**</include>
117116
<include>org.threeten.**</include>
118117
<include>org.codehaus.**</include>

google-cloud-clients/google-cloud-core/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727
<artifactId>junit</artifactId>
2828
<scope>test</scope>
2929
</dependency>
30-
<dependency>
31-
<groupId>joda-time</groupId>
32-
<artifactId>joda-time</artifactId>
33-
<scope>compile</scope>
34-
</dependency>
3530
<dependency>
3631
<groupId>com.google.http-client</groupId>
3732
<artifactId>google-http-client</artifactId>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.threeten.bp.Instant;
2727
import org.threeten.bp.LocalDateTime;
2828
import org.threeten.bp.ZoneOffset;
29-
import org.threeten.bp.chrono.IsoChronology;
3029
import org.threeten.bp.format.DateTimeFormatter;
3130

3231
/**
@@ -47,7 +46,7 @@ public final class Timestamp implements Comparable<Timestamp>, Serializable {
4746
new Timestamp(253402300799L, (int) TimeUnit.SECONDS.toNanos(1) - 1);
4847

4948
private static final DateTimeFormatter format =
50-
DateTimeFormatter.ISO_LOCAL_DATE_TIME.withChronology(IsoChronology.INSTANCE);
49+
DateTimeFormatter.ISO_LOCAL_DATE_TIME;
5150

5251
private final long seconds;
5352
private final int nanos;

google-cloud-clients/google-cloud-dns/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@
6060
<artifactId>objenesis</artifactId>
6161
<scope>test</scope>
6262
</dependency>
63+
<dependency>
64+
<groupId>joda-time</groupId>
65+
<artifactId>joda-time</artifactId>
66+
<scope>test</scope>
67+
</dependency>
6368
</dependencies>
6469
</project>

google-cloud-clients/google-cloud-dns/src/main/java/com/google/cloud/dns/ChangeRequestInfo.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import com.google.common.base.MoreObjects;
2727
import com.google.common.collect.ImmutableList;
2828
import com.google.common.collect.Lists;
29+
import org.threeten.bp.Instant;
30+
import org.threeten.bp.ZoneOffset;
31+
import org.threeten.bp.format.DateTimeFormatter;
32+
2933
import java.io.Serializable;
3034
import java.util.LinkedList;
3135
import java.util.List;
3236
import java.util.Objects;
33-
import org.joda.time.DateTime;
34-
import org.joda.time.format.ISODateTimeFormat;
3537

3638
/**
3739
* A class representing an atomic update to a collection of {@link RecordSet}s within a {@code
@@ -328,7 +330,8 @@ Change toPb() {
328330
}
329331
// set timestamp
330332
if (getStartTimeMillis() != null) {
331-
pb.setStartTime(ISODateTimeFormat.dateTime().withZoneUTC().print(getStartTimeMillis()));
333+
pb.setStartTime(DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC)
334+
.format(Instant.ofEpochMilli(getStartTimeMillis())));
332335
}
333336
// set status
334337
if (status() != null) {
@@ -347,7 +350,8 @@ static ChangeRequestInfo fromPb(Change pb) {
347350
builder.setGeneratedId(pb.getId());
348351
}
349352
if (pb.getStartTime() != null) {
350-
builder.setStartTime(DateTime.parse(pb.getStartTime()).getMillis());
353+
builder.setStartTime(
354+
DateTimeFormatter.ISO_DATE_TIME.parse(pb.getStartTime(), Instant.FROM).toEpochMilli());
351355
}
352356
if (pb.getStatus() != null) {
353357
// we are assuming that status indicated in pb is a lower case version of the enum name

google-cloud-clients/google-cloud-dns/src/main/java/com/google/cloud/dns/ZoneInfo.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222
import com.google.common.base.MoreObjects;
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.collect.Lists;
25+
import org.threeten.bp.Instant;
26+
import org.threeten.bp.LocalDateTime;
27+
import org.threeten.bp.ZoneOffset;
28+
import org.threeten.bp.format.DateTimeFormatter;
29+
import org.threeten.bp.temporal.TemporalAccessor;
30+
import org.threeten.bp.temporal.TemporalField;
31+
2532
import java.io.Serializable;
2633
import java.math.BigInteger;
2734
import java.util.List;
2835
import java.util.Objects;
29-
import org.joda.time.DateTime;
30-
import org.joda.time.format.ISODateTimeFormat;
3136

3237
/**
3338
* A {@code Zone} represents a DNS zone hosted by the Google Cloud DNS service. A zone is a subtree
@@ -38,6 +43,9 @@
3843
public class ZoneInfo implements Serializable {
3944

4045
private static final long serialVersionUID = -5313169712036079818L;
46+
private static final DateTimeFormatter DATE_TIME_FORMATTER =
47+
DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC);
48+
4149
private final String name;
4250
private final String generatedId;
4351
private final Long creationTimeMillis;
@@ -233,8 +241,8 @@ ManagedZone toPb() {
233241
pb.setNameServers(this.nameServers); // do use real attribute value which may be null
234242
pb.setNameServerSet(this.getNameServerSet());
235243
if (this.getCreationTimeMillis() != null) {
236-
pb.setCreationTime(
237-
ISODateTimeFormat.dateTime().withZoneUTC().print(this.getCreationTimeMillis()));
244+
pb.setCreationTime(DATE_TIME_FORMATTER
245+
.format(Instant.ofEpochMilli(this.getCreationTimeMillis())));
238246
}
239247
return pb;
240248
}
@@ -257,7 +265,8 @@ static ZoneInfo fromPb(ManagedZone pb) {
257265
builder.setNameServerSet(pb.getNameServerSet());
258266
}
259267
if (pb.getCreationTime() != null) {
260-
builder.setCreationTimeMillis(DateTime.parse(pb.getCreationTime()).getMillis());
268+
builder.setCreationTimeMillis(
269+
DATE_TIME_FORMATTER.parse(pb.getCreationTime(), Instant.FROM).toEpochMilli());
261270
}
262271
return builder.build();
263272
}

google-cloud-clients/google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ProjectInfo.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
import com.google.cloud.StringEnumValue;
2626
import com.google.common.collect.ImmutableMap;
2727
import com.google.common.collect.Maps;
28+
import org.threeten.bp.Instant;
29+
import org.threeten.bp.ZoneOffset;
30+
import org.threeten.bp.format.DateTimeFormatter;
31+
2832
import java.io.Serializable;
2933
import java.util.HashMap;
3034
import java.util.Map;
3135
import java.util.Objects;
32-
import org.joda.time.DateTime;
33-
import org.joda.time.format.ISODateTimeFormat;
3436

3537
/**
3638
* A Google Cloud Resource Manager project metadata object. A Project is a high-level Google Cloud
@@ -39,6 +41,8 @@
3941
*/
4042
public class ProjectInfo implements Serializable {
4143

44+
public static final DateTimeFormatter DATE_TIME_FORMATTER =
45+
DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC);
4246
private static final long serialVersionUID = 9148970963697734236L;
4347
private final String name;
4448
private final String projectId;
@@ -390,7 +394,8 @@ com.google.api.services.cloudresourcemanager.model.Project toPb() {
390394
projectPb.setLifecycleState(state.toString());
391395
}
392396
if (createTimeMillis != null) {
393-
projectPb.setCreateTime(ISODateTimeFormat.dateTime().withZoneUTC().print(createTimeMillis));
397+
projectPb.setCreateTime(DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC)
398+
.format(Instant.ofEpochMilli(createTimeMillis)));
394399
}
395400
if (parent != null) {
396401
projectPb.setParent(parent.toPb());
@@ -411,7 +416,8 @@ static ProjectInfo fromPb(com.google.api.services.cloudresourcemanager.model.Pro
411416
builder.setState(State.valueOf(projectPb.getLifecycleState()));
412417
}
413418
if (projectPb.getCreateTime() != null) {
414-
builder.setCreateTimeMillis(DateTime.parse(projectPb.getCreateTime()).getMillis());
419+
builder.setCreateTimeMillis(
420+
DATE_TIME_FORMATTER.parse(projectPb.getCreateTime(), Instant.FROM).toEpochMilli());
415421
}
416422
if (projectPb.getParent() != null) {
417423
builder.setParent(ResourceId.fromPb(projectPb.getParent()));

google-cloud-clients/google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/testing/LocalResourceManagerHelper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static java.net.HttpURLConnection.HTTP_OK;
2222

2323
import com.google.api.client.json.JsonFactory;
24+
import com.google.api.client.util.DateTime;
2425
import com.google.api.services.cloudresourcemanager.model.Binding;
2526
import com.google.api.services.cloudresourcemanager.model.Operation;
2627
import com.google.api.services.cloudresourcemanager.model.Policy;
@@ -40,6 +41,10 @@
4041
import com.sun.net.httpserver.HttpExchange;
4142
import com.sun.net.httpserver.HttpHandler;
4243
import com.sun.net.httpserver.HttpServer;
44+
import org.threeten.bp.Instant;
45+
import org.threeten.bp.ZoneOffset;
46+
import org.threeten.bp.format.DateTimeFormatter;
47+
4348
import java.io.IOException;
4449
import java.io.InputStream;
4550
import java.io.OutputStream;
@@ -61,7 +66,6 @@
6166
import java.util.regex.Matcher;
6267
import java.util.regex.Pattern;
6368
import java.util.zip.GZIPInputStream;
64-
import org.joda.time.format.ISODateTimeFormat;
6569

6670
/**
6771
* Utility to create a local Resource Manager mock for testing.
@@ -443,7 +447,8 @@ synchronized Response create(Project project) {
443447
} else {
444448
project.setLifecycleState("ACTIVE");
445449
project.setProjectNumber(Math.abs(PROJECT_NUMBER_GENERATOR.nextLong() % Long.MAX_VALUE));
446-
project.setCreateTime(ISODateTimeFormat.dateTime().print(System.currentTimeMillis()));
450+
project.setCreateTime(DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC)
451+
.format(Instant.ofEpochMilli(System.currentTimeMillis())));
447452
if (projects.putIfAbsent(project.getProjectId(), project) != null) {
448453
return Error.ALREADY_EXISTS.response(
449454
"A project with the same project ID (" + project.getProjectId() + ") already exists.");

0 commit comments

Comments
 (0)