Skip to content

Instantly share code, notes, and snippets.

@eirikbakke
Created June 10, 2015 21:20
Show Gist options
  • Save eirikbakke/f006e0c5c15ffc4f86b0 to your computer and use it in GitHub Desktop.
Save eirikbakke/f006e0c5c15ffc4f86b0 to your computer and use it in GitHub Desktop.
private static final ZoneId UTC_ZONEID = ZoneId.of("UTC");
public double toDateDouble(Object value) {
long time = toDateLong(value);
time += getToLocalTimeZoneOffset(time);
final LocalDateTime ldt =
LocalDateTime.ofInstant(Instant.ofEpochMilli(time), UTC_ZONEID);
final long datePart = ldt.toLocalDate().toEpochDay() * (long) MILLISECONDS_PER_DAY +
MILLIS_BETWEEN_EPOCH_AND_1900;
final long timePart = ldt.toLocalTime().toNanoOfDay() / 1000000L;
final long ret = (datePart >= 0) ? (datePart + timePart) : (datePart - timePart);
return ret / MILLISECONDS_PER_DAY;
}
public long fromDateDouble(double value) {
long datePart = ((long) value) * (long) MILLISECONDS_PER_DAY;
long timePart = Math.round(Math.abs(value % 1.0) * MILLISECONDS_PER_DAY);
long time = datePart + timePart;
time -= MILLIS_BETWEEN_EPOCH_AND_1900;
time -= getFromLocalTimeZoneOffset(time);
return time;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment