TimeUtil { public static @Nullable Instant fromCloudTime(String time) { Matcher matcher = TIME_PATTERN.matcher(time); if (!matcher.matches()) { return null; } int year = Integer.parseInt(matcher.group(1)); int month = Integer.parseInt(matcher.group(2)); int day = Integer.parseInt(matcher.group(3)); int hour = Integer.parseInt(matcher.group(4)); int minute = Integer.parseInt(matcher.group(5)); int second = Integer.parseInt(matcher.group(6)); int millis = computeMillis(matcher.group(7)); return new DateTime( year, month, day, hour, minute, second, millis, ISOChronology.getInstanceUTC()) .toInstant(); } 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 fromCloudTimeShouldParseTimeStrings() { assertEquals(new Instant(0), fromCloudTime("1970-01-01T00:00:00Z")); assertEquals(new Instant(1), fromCloudTime("1970-01-01T00:00:00.001Z")); assertEquals(new Instant(1), fromCloudTime("1970-01-01T00:00:00.001000Z")); assertEquals(new Instant(1), fromCloudTime("1970-01-01T00:00:00.001001Z")); assertEquals(new Instant(1), fromCloudTime("1970-01-01T00:00:00.001000000Z")); assertEquals(new Instant(1), fromCloudTime("1970-01-01T00:00:00.001000001Z")); assertEquals(new Instant(0), fromCloudTime("1970-01-01T00:00:00.0Z")); assertEquals(new Instant(0), fromCloudTime("1970-01-01T00:00:00.00Z")); assertEquals(new Instant(420), fromCloudTime("1970-01-01T00:00:00.42Z")); assertEquals(new Instant(300), fromCloudTime("1970-01-01T00:00:00.3Z")); assertEquals(new Instant(20), fromCloudTime("1970-01-01T00:00:00.02Z")); assertNull(fromCloudTime("")); assertNull(fromCloudTime("1970-01-01T00:00:00")); assertNull(fromCloudTime("1970-01-01T00:00:00.1e3Z")); }