@Override public LocalDate convert(ZonedDateTime source) { return source.toLocalDate(); } }
import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class DateUtils { public static Date asDate(LocalDate localDate) { return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); } public static Date asDate(LocalDateTime localDateTime) { return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } public static LocalDate asLocalDate(Date date) { return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate(); } public static LocalDateTime asLocalDateTime(Date date) { return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime(); } }
@Override public LocalDate convert(Calendar source) { return calendarToZonedDateTime(source).toLocalDate(); } }
@Override public LocalDate convert(Calendar source) { return calendarToZonedDateTime(source).toLocalDate(); } }
@Override LocalDate getDatePart() { return value.toLocalDate(); }
@Override public LocalDate convert(ZonedDateTime source) { return source.toLocalDate(); } }
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); }
/** * Warning: relies on default timezone! */ public static String formatDate(Date d) { return d.toInstant().atZone(ZoneId.systemDefault()).toLocalDate().toString(); }
/** * Warning: relies on default timezone! * * @since 7.6 */ public static String formatDate(Instant d) { return d.atZone(ZoneId.systemDefault()).toLocalDate().toString(); }
private static LocalDate parse(final DateTimeFormatter formatter, final String value) { try { Instant epoch = Instant.ofEpochMilli(Long.parseLong(value)); ZonedDateTime zonedDate = epoch.atZone( Optional.ofNullable(formatter.getZone()) .orElse(ZoneId.systemDefault()) ); return zonedDate.toLocalDate(); } catch (NumberFormatException ex) { return LocalDate.parse(value, formatter); } }
private static LocalDate getLocalDateFromEpochTime(String fieldName, Object coercedValue) { Long date = DataTypeUtils.toLong(coercedValue, fieldName); return Instant.ofEpochMilli(date).atZone(ZoneId.systemDefault()).toLocalDate(); }
@Override public LocalDate decode(final BsonReader reader, final DecoderContext decoderContext) { return Instant.ofEpochMilli(validateAndReadDateTime(reader)).atZone(ZoneOffset.UTC).toLocalDate(); }
public static int pack(Date date) { return pack(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()); }
@Override public void buildDynamicSection(StringBuilder sb, ZoneId zoneId) { sb.append("Period: ").append(getDurationLabel()); addReportSpecificAtTime(sb, zoneId); if (durationType == ReportDurationType.CUSTOM) { sb.append("<br>"); ZonedDateTime date; date = getZonedFromTs(startTs, zoneId); sb.append("Start date: ").append(date.toLocalDate()).append("<br>"); date = ZonedDateTime.ofInstant(Instant.ofEpochMilli(endTs), zoneId); sb.append("End date: ").append(date.toLocalDate()).append("<br>"); } }
ZoneId zoneId_Norway = ZoneId.of( "Europe/Oslo" ); ZonedDateTime zdt_Norway = ZonedDateTime.of( 1985 , 1 , 1 , 3 , 2 , 1 , 0 , zoneId_Norway ); ZoneId zoneId_NewYork = ZonedId.of( "America/New_York" ); ZonedDateTime zdt_NewYork = zdt_Norway.withZoneSameInstant( zoneId_NewYork ); ZonedDateTime zdt_Utc = zdt_Norway.withZoneSameInstant( ZoneOffset.UTC ); // Or, next line is similar. Instant instant = zdt_Norway.toInstant(); // Instant is always in UTC. LocalDate localDate_Norway = zdt_Norway.toLocalDate();
private static String dateTimeToDate(String timestamp, TimeZone timeZone) { Date date = parseDateTime(timestamp); return date.toInstant().atZone(timeZone.toZoneId()).toLocalDate().toString(); }
@Override public <X> LocalDate wrap(X value, WrapperOptions options) { if ( value == null ) { return null; } if ( LocalDate.class.isInstance( value ) ) { return (LocalDate) value; } if ( Timestamp.class.isInstance( value ) ) { final Timestamp ts = (Timestamp) value; return LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ).toLocalDate(); } if ( Long.class.isInstance( value ) ) { final Instant instant = Instant.ofEpochMilli( (Long) value ); return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ).toLocalDate(); } if ( Calendar.class.isInstance( value ) ) { final Calendar calendar = (Calendar) value; return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toLocalDate(); } if ( Date.class.isInstance( value ) ) { if ( java.sql.Date.class.isInstance( value ) ) { return ((java.sql.Date) value).toLocalDate(); } else { return Instant.ofEpochMilli( ((Date) value).getTime() ).atZone( ZoneId.systemDefault() ).toLocalDate(); } } throw unknownWrap( value.getClass() ); }
private boolean isValidOn(EdgeIteratorState edge, long instant) { GtfsStorage.EdgeType edgeType = flagEncoder.getEdgeType(edge.getFlags()); if (edgeType == GtfsStorage.EdgeType.BOARD || edgeType == GtfsStorage.EdgeType.ALIGHT) { final int validityId = flagEncoder.getValidityId(edge.getFlags()); final GtfsStorage.Validity validity = realtimeFeed.getValidity(validityId); final int trafficDay = (int) ChronoUnit.DAYS.between(validity.start, Instant.ofEpochMilli(instant).atZone(validity.zoneId).toLocalDate()); return trafficDay >= 0 && validity.validity.get(trafficDay); } else { return true; } }
public boolean isTickTime(ZonedDateTime currentDateTime) { LocalDate userDate = currentDateTime.withZoneSameInstant(tzName).toLocalDate(); int dayOfWeek = userDate.getDayOfWeek().getValue(); return ArrayUtil.contains(days, dayOfWeek); }