@Override public LocalDateTime convert(ZonedDateTime source) { return source.toLocalDateTime(); } }
@Override public LocalDateTime convert(Calendar source) { return calendarToZonedDateTime(source).toLocalDateTime(); } }
@Override public LocalDateTime convert(ZonedDateTime source) { return source.toLocalDateTime(); } }
@Override public LocalDateTime convert(Calendar source) { return calendarToZonedDateTime(source).toLocalDateTime(); } }
/** * Convert date to LocalDateTime using system default zone. * * @param date Date to be transformed * @return converted date to LocalDateTime */ public static LocalDateTime convert(Date date) { if (date == null) return null; return date.toInstant().atZone(defaultZoneId).toLocalDateTime(); }
private static String dateTime(long epochMillis) { DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME; LocalDateTime date = Instant.ofEpochMilli(epochMillis).atZone(ZoneId.systemDefault()).toLocalDateTime(); return formatter.format(date); }
private static LocalDateTime getEndTimeOf(Path file) { try { long lastModifiedMillis = file.toFile().lastModified(); return Instant.ofEpochMilli(lastModifiedMillis).atZone(ZoneId.systemDefault()) .toLocalDateTime(); } catch (Exception e) { LOGGER.error("Unable to determine lastModified time", e); return LocalDateTime.MAX; } }
public static LocalDateTime parseTime(String dateString) { if (dateString == null) { return null; } try { SimpleDateFormat df = new SimpleDateFormat(ExportLogsCommand.FORMAT); return df.parse(dateString).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } catch (ParseException e) { try { SimpleDateFormat df = new SimpleDateFormat(ExportLogsCommand.ONLY_DATE_FORMAT); return df.parse(dateString).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } catch (ParseException e1) { return null; } } } }
private static LocalDateTime getLocalDateTimeFromEpochTime(String fieldName, Object coercedValue) { Long date = DataTypeUtils.toLong(coercedValue, fieldName); return Instant.ofEpochMilli(date).atZone(ZoneId.systemDefault()).toLocalDateTime(); }
@Override public LocalDateTime decode(final BsonReader reader, final DecoderContext decoderContext) { return Instant.ofEpochMilli(validateAndReadDateTime(reader)).atZone(ZoneOffset.UTC).toLocalDateTime(); }
public static long pack(Date date) { if (date == null) { return missingValueIndicator(); } return pack(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()); }
private void copyDateTime( String key, CalendarEventModel.CalendarEventTime dateTime, Map<String, Object> graphCalendar) { Map<String, String> graphDateTime = new HashMap<>(); graphDateTime.put( "dateTime", dateTime.getDateTime().atZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime().toString()); graphDateTime.put("timeZone", "UTC"); graphCalendar.put(key, graphDateTime); }
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(); } }
public long parse(final String text, final ZoneId zoneId) { TemporalAccessor parsed = formatter.parseBest( text, ZonedDateTime::from, LocalDateTime::from); if (parsed == null) { throw new KsqlException("text value: " + text + "cannot be parsed into a timestamp"); } if (parsed instanceof LocalDateTime) { parsed = ((LocalDateTime) parsed).atZone(zoneId); } final LocalDateTime dateTime = ((ZonedDateTime) parsed) .withZoneSameInstant(ZoneId.systemDefault()) .toLocalDateTime(); return Timestamp.valueOf(dateTime).getTime(); }
/** * Returns a DateTimeColumn where each value is the LocalDateTime represented by the values in this column * <p> * The values in this column must be longs that represent the time in milliseconds from the epoch as in standard * Java date/time calculations * * @param offset The ZoneOffset to use in the calculation * @return A column of LocalDateTime values */ public DateTimeColumn asDateTimes(ZoneOffset offset) { DateTimeColumn column = DateTimeColumn.create(name() + ": date time"); for (int i = 0; i < size(); i++) { column.append(Instant.ofEpochMilli(getLong(i)).atZone(offset).toLocalDateTime()); } return column; }
@Override protected DateTimeValue selectDateTime( AnyValue datetime ) { if ( datetime instanceof DateTimeValue ) { DateTimeValue value = (DateTimeValue) datetime; ZoneId zone = optionalTimezone(); return zone == null ? value : new DateTimeValue( ZonedDateTime.of( value.temporal().toLocalDateTime(), zone ) ); } if ( datetime instanceof LocalDateTimeValue ) { return new DateTimeValue( ZonedDateTime.of( ((LocalDateTimeValue) datetime).temporal(), timezone() ) ); } throw new UnsupportedTemporalUnitException( "Cannot select datetime from: " + datetime ); } };
@Override public boolean equals( Value other ) { if ( other instanceof DateTimeValue ) { ZonedDateTime that = ((DateTimeValue) other).value; boolean res = value.toLocalDateTime().equals( that.toLocalDateTime() ); if ( res ) { ZoneId thisZone = value.getZone(); ZoneId thatZone = that.getZone(); boolean thisIsOffset = thisZone instanceof ZoneOffset; boolean thatIsOffset = thatZone instanceof ZoneOffset; if ( thisIsOffset && thatIsOffset ) { res = thisZone.equals( thatZone ); } else if ( !thisIsOffset && !thatIsOffset ) { res = TimeZones.map( thisZone.getId() ) == TimeZones.map( thatZone.getId() ); } else { res = false; } } return res; } return false; }
@Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { if (arguments.length != 0) { throw new UDFArgumentLengthException( "The function CURRENT_TIMESTAMP does not take any arguments, but found " + arguments.length); } if (currentTimestamp == null) { SessionState ss = SessionState.get(); ZonedDateTime dateTime = ss.getQueryCurrentTimestamp().atZone( ss.getConf().getLocalTimeZone()); currentTimestamp = new TimestampWritableV2( Timestamp.valueOf(dateTime.toLocalDateTime().toString())); } return PrimitiveObjectInspectorFactory.writableTimestampObjectInspector; }
public static Timestamp stringToTimestamp(String s) { s = s.trim(); // Handle simpler cases directly avoiding exceptions if (s.length() == DATE_LENGTH) { // Its a date! return Timestamp.ofEpochMilli(Date.valueOf(s).toEpochMilli()); } try { return Timestamp.valueOf(s); } catch (IllegalArgumentException eT) { // Try zoned timestamp try { return Timestamp.valueOf( TimestampTZUtil.parse(s).getZonedDateTime().toLocalDateTime().toString()); } catch (IllegalArgumentException | DateTimeParseException eTZ) { // Last attempt return Timestamp.ofEpochMilli(Date.valueOf(s).toEpochMilli()); } } } }
@Override public void writeDateTime( ZonedDateTime zonedDateTime ) throws IOException { long epochSecondLocal = zonedDateTime.toLocalDateTime().toEpochSecond( UTC ); int nano = zonedDateTime.getNano(); ZoneId zone = zonedDateTime.getZone(); if ( zone instanceof ZoneOffset ) { int offsetSeconds = ((ZoneOffset) zone).getTotalSeconds(); packStructHeader( DATE_TIME_WITH_ZONE_OFFSET_SIZE, DATE_TIME_WITH_ZONE_OFFSET ); pack( epochSecondLocal ); pack( nano ); pack( offsetSeconds ); } else { String zoneId = zone.getId(); packStructHeader( DATE_TIME_WITH_ZONE_NAME_SIZE, DATE_TIME_WITH_ZONE_NAME ); pack( epochSecondLocal ); pack( nano ); pack( zoneId ); } } }