Date input = new Date(); Instant instant = input.toInstant(); ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
Date input = new Date(); Instant instant = input.toInstant(); ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault()); LocalDate date = zdt.toLocalDate();
/** * Compute the number of calendar days elapsed since the given date. * As it's only the calendar days difference that matter, "11.00pm" to "2.00am the day after" returns 1, * even if there are only 3 hours between. As well as "10am" to "2pm" both on the same day, returns 0. */ @Restricted(NoExternalUse.class) public static long daysBetween(@Nonnull Date a, @Nonnull Date b){ LocalDate aLocal = a.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate bLocal = b.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); return ChronoUnit.DAYS.between(aLocal, bLocal); }
/** * format unix time to string * * @param unixTime unix time * @param pattern date format pattern * @return return string date */ public static String toString(long unixTime, String pattern) { return Instant.ofEpochSecond(unixTime).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(pattern)); }
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
public static ParameterizedType makeClassName(String baseName, Optional<String> suffix) { String className = baseName + "_" + suffix.orElseGet(() -> Instant.now().atZone(UTC).format(TIMESTAMP_FORMAT)) + "_" + CLASS_ID.incrementAndGet(); return typeFromJavaClassName("com.facebook.presto.$gen." + toJavaIdentifierString(className)); }
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
@Test public void shouldHandleLastModifiedWithHttp304() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); ResponseEntity<String> returnValue = ResponseEntity.ok().lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, null, oneMinuteAgo); }
@Before public void before() { time = 1505892010L; date = new Date(1505892470110L); localDateTime = LocalDateTime.from(Instant.ofEpochSecond(time).atZone(ZoneId.systemDefault())); }
@Test // SPR-13626 public void shouldHandleIfNoneMatchIfUnmodifiedSince() throws Exception { String etagValue = "\"some-etag\""; servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); ZonedDateTime dateTime = ofEpochMilli(new Date().getTime()).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, -1); }
private static long epochDaysInZone(TimeZoneKey timeZoneKey, long instant) { return LocalDate.from(Instant.ofEpochMilli(instant).atZone(ZoneId.of(timeZoneKey.getId()))).toEpochDay(); }
@Test public void shouldNotFailPreconditionForPutRequests() throws Exception { servletRequest.setMethod("PUT"); ZonedDateTime dateTime = ofEpochMilli(new Date().getTime()).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); long justModified = dateTime.plus(1, ChronoUnit.SECONDS).toEpochSecond() * 1000; ResponseEntity<String> returnValue = ResponseEntity.ok() .lastModified(justModified).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.OK, null, null, justModified); }
@Test public void shouldHandleETagAndLastModifiedWithHttp304() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); String etagValue = "\"deadb33f8badf00d\""; ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); ResponseEntity<String> returnValue = ResponseEntity.ok() .eTag(etagValue).lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, oneMinuteAgo); }
@Test public void shouldHandleChangedETagAndLastModified() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); String etagValue = "\"deadb33f8badf00d\""; String changedEtagValue = "\"changed-etag-value\""; ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); ResponseEntity<String> returnValue = ResponseEntity.ok() .eTag(changedEtagValue).lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.OK, null, changedEtagValue, oneMinuteAgo); }