/** * Obtains a clock that returns the current instant using the best available * system clock, converting to date and time using the default time-zone. * <p> * This clock is based on the best available system clock. * This may use {@link System#currentTimeMillis()}, or a higher resolution * clock if one is available. * <p> * Using this method hard codes a dependency to the default time-zone into your application. * It is recommended to avoid this and use a specific time-zone whenever possible. * The {@link #systemUTC() UTC clock} should be used when you need the current instant * without the date or time. * <p> * The returned implementation is immutable, thread-safe and {@code Serializable}. * It is equivalent to {@code system(ZoneId.systemDefault())}. * * @return a clock that uses the best available system clock in the default zone, not null * @see ZoneId#systemDefault() */ public static Clock systemDefaultZone() { return new SystemClock(ZoneId.systemDefault()); }
/** * Obtains a clock that returns the current instant using the best available * system clock, converting to date and time using the default time-zone. * <p> * This clock is based on the best available system clock. * This may use {@link System#currentTimeMillis()}, or a higher resolution * clock if one is available. * <p> * Using this method hard codes a dependency to the default time-zone into your application. * It is recommended to avoid this and use a specific time-zone whenever possible. * The {@link #systemUTC() UTC clock} should be used when you need the current instant * without the date or time. * <p> * The returned implementation is immutable, thread-safe and {@code Serializable}. * It is equivalent to {@code system(ZoneId.systemDefault())}. * * @return a clock that uses the best available system clock in the default zone, not null * @see ZoneId#systemDefault() */ public static Clock systemDefaultZone() { return new SystemClock(ZoneId.systemDefault()); }
public OffsetDateTime deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException, JsonProcessingException { String date = jsonparser.getText(); OffsetDateTime formattedDate; Pattern datePatt = Pattern.compile("^/Date\\((\\d+)([+-]\\d+)?\\)/$"); Matcher m = datePatt.matcher(date); if (m.matches()) { Long l = Long.parseLong(m.group(1)); formattedDate = Instant.ofEpochMilli(l).atZone(ZoneId.systemDefault()).toOffsetDateTime(); } else { throw new IllegalArgumentException("Wrong date format"); } return formattedDate; } }
@Nonnull @Override public LocalDateTime convert(Date source) { return ofInstant(toInstant(source), systemDefault()); } }
public LocalDate deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException, JsonProcessingException { String date = jsonparser.getText(); LocalDate formattedDate; Pattern datePatt = Pattern.compile("^/Date\\((\\d+)([+-]\\d+)?\\)/$"); Matcher m = datePatt.matcher(date); if (m.matches()) { Long l = Long.parseLong(m.group(1)); formattedDate = Instant.ofEpochMilli(l).atZone(ZoneId.systemDefault()).toLocalDate(); } else { throw new IllegalArgumentException("Wrong date format"); } return formattedDate; } }
@Nonnull @Override public LocalTime convert(Date source) { return ofInstant(ofEpochMilli(source.getTime()), systemDefault()).toLocalTime(); } }
@Nullable public static Long localDateTimeToLong(@Nullable LocalDateTime d) { if (d == null) { return null; } return d.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); }
@Nullable public static LocalDateTime longToLocalDateTime(@Nullable Long l) { if (l == null) { return null; } return Instant.ofEpochMilli(l).atZone(ZoneId.systemDefault()).toLocalDateTime(); }
@Nonnull @Override public LocalDate convert(Date source) { return ofInstant(ofEpochMilli(source.getTime()), systemDefault()).toLocalDate(); } }
@Nonnull @Override public Date convert(LocalDateTime source) { return toDate(source.atZone(systemDefault()).toInstant()); } }
@Nonnull @Override public Date convert(LocalDate source) { return toDate(source.atStartOfDay(systemDefault()).toInstant()); } }
@Nonnull @Override public Date convert(Instant source) { return toDate(source.atZone(systemDefault()).toInstant()); } }
@Nonnull @Override public java.time.LocalDateTime convert(LocalDateTime source) { Date date = toDate(source.atZone(ZoneId.systemDefault()).toInstant()); return Jsr310Converters.DateToLocalDateTimeConverter.INSTANCE.convert(date); } }
default java.util.Date map(org.threeten.bp.LocalDate value) { return value != null ? DateTimeUtils.toDate(value.atStartOfDay(ZoneId.systemDefault()).toInstant()) : null; } }
private Duration getDifferenceBetweenTimeAndNow(long timeStart) { LocalDateTime today = LocalDateTime.now(); LocalDateTime otherTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStart), ZoneId.systemDefault()); return Duration.between(otherTime, today); }
/** * Call on background thread to eagerly load all zones. Starts with loading * {@link ZoneId#systemDefault()} which is the one most likely to be used. */ @WorkerThread public static void cacheZones() { ZoneId.systemDefault().getRules(); for (String zoneId : ZoneRulesProvider.getAvailableZoneIds()) { ZoneRulesProvider.getRules(zoneId, true); } }
@Nonnull @Override public Date convert(LocalTime source) { return toDate(source.atDate(LocalDate.now()).atZone(systemDefault()).toInstant()); } }