TimeUtil { public static String toCloudTime(ReadableInstant instant) { DateTime time = new DateTime(instant); int millis = time.getMillisOfSecond(); if (millis == 0) { return String.format( "%04d-%02d-%02dT%02d:%02d:%02dZ", time.getYear(), time.getMonthOfYear(), time.getDayOfMonth(), time.getHourOfDay(), time.getMinuteOfHour(), time.getSecondOfMinute()); } else { return String.format( "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", time.getYear(), time.getMonthOfYear(), time.getDayOfMonth(), time.getHourOfDay(), time.getMinuteOfHour(), time.getSecondOfMinute(), millis); } } private  TimeUtil(); static String toCloudTime(ReadableInstant instant); static @Nullable Instant fromCloudTime(String time); static String toCloudDuration(ReadableDuration duration); static @Nullable Duration fromCloudDuration(String duration);  }
@Test public void toCloudTimeShouldPrintTimeStrings() { assertEquals("1970-01-01T00:00:00Z", toCloudTime(new Instant(0))); assertEquals("1970-01-01T00:00:00.001Z", toCloudTime(new Instant(1))); }