@@ -111,7 +111,7 @@ public class ImpersonatedCredentials extends GoogleCredentials
111111
112112 private transient HttpTransportFactory transportFactory ;
113113
114- @ VisibleForTesting transient Calendar calendar = Calendar . getInstance () ;
114+ private transient Calendar calendar ;
115115
116116 /**
117117 * @param sourceCredentials the source credential used to acquire the impersonated credentials. It
@@ -432,6 +432,25 @@ public GoogleCredentials createScoped(Collection<String> scopes) {
432432 .build ();
433433 }
434434
435+ /**
436+ * Clones the impersonated credentials with a new calendar.
437+ *
438+ * @param calendar the calendar that will be used by the new ImpersonatedCredentials instance when
439+ * parsing the received expiration time of the refreshed access token
440+ * @return the cloned impersonated credentials with the given custom calendar
441+ */
442+ public ImpersonatedCredentials createWithCustomCalendar (Calendar calendar ) {
443+ return toBuilder ()
444+ .setScopes (this .scopes )
445+ .setLifetime (this .lifetime )
446+ .setDelegates (this .delegates )
447+ .setHttpTransportFactory (this .transportFactory )
448+ .setQuotaProjectId (this .quotaProjectId )
449+ .setIamEndpointOverride (this .iamEndpointOverride )
450+ .setCalendar (calendar )
451+ .build ();
452+ }
453+
435454 @ Override
436455 protected Map <String , List <String >> getAdditionalHeaders () {
437456 Map <String , List <String >> headers = super .getAdditionalHeaders ();
@@ -454,6 +473,7 @@ private ImpersonatedCredentials(Builder builder) {
454473 this .quotaProjectId = builder .quotaProjectId ;
455474 this .iamEndpointOverride = builder .iamEndpointOverride ;
456475 this .transportFactoryClassName = this .transportFactory .getClass ().getName ();
476+ this .calendar = builder .getCalendar ();
457477 if (this .delegates == null ) {
458478 this .delegates = new ArrayList <String >();
459479 }
@@ -610,6 +630,7 @@ public static class Builder extends GoogleCredentials.Builder {
610630 private HttpTransportFactory transportFactory ;
611631 private String quotaProjectId ;
612632 private String iamEndpointOverride ;
633+ private Calendar calendar = Calendar .getInstance ();
613634
614635 protected Builder () {}
615636
@@ -682,6 +703,15 @@ public Builder setIamEndpointOverride(String iamEndpointOverride) {
682703 return this ;
683704 }
684705
706+ public Builder setCalendar (Calendar calendar ) {
707+ this .calendar = calendar ;
708+ return this ;
709+ }
710+
711+ public Calendar getCalendar () {
712+ return this .calendar ;
713+ }
714+
685715 public ImpersonatedCredentials build () {
686716 return new ImpersonatedCredentials (this );
687717 }
0 commit comments