Refine search
/** * Converts local date time to epoh milliseconds. */ public static long toMilliseconds(final LocalDateTime localDateTime) { return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); }
public static long dateToLong(Object datelike) { if (datelike instanceof Date) { Date date = (Date) datelike; return date.getTime(); } else if (datelike instanceof Calendar) { Calendar calendar = (Calendar) datelike; return calendar.getTimeInMillis(); } else if (datelike instanceof Instant) { Instant instant = (Instant) datelike; return instant.toEpochMilli(); } else if (datelike instanceof LocalDateTime) { LocalDateTime date = (LocalDateTime) datelike; return date.atZone(ZoneId.of("UTC")).toInstant().toEpochMilli(); } else if (datelike instanceof LocalDate) { LocalDate date = (LocalDate) datelike; return date.atStartOfDay(ZoneId.of("UTC")).toInstant().toEpochMilli(); } else { throw new IllegalArgumentException("Illegal argument " + datelike + ". Expected a Number, Date, Instant, LocalDateTime, or LocalDate"); } }
@Override protected DateTime evaluate(FunctionArgs args, EvaluationContext context, DateTimeZone timezone) { final Object datish = value.required(args, context); if (datish instanceof DateTime) { return (DateTime) datish; } if (datish instanceof Date) { return new DateTime(datish); } if (datish instanceof ZonedDateTime) { final ZonedDateTime zonedDateTime = (ZonedDateTime) datish; final DateTimeZone timeZone = DateTimeZone.forID(zonedDateTime.getZone().getId()); return new DateTime(zonedDateTime.toInstant().toEpochMilli(), timeZone); } return null; }
private DateTimeValue( ZonedDateTime value ) { ZoneId zone = value.getZone(); if ( zone instanceof ZoneOffset ) { this.value = value; } else { // Do a 2-way lookup of the zone to make sure we only use the new name of renamed zones ZoneId mappedZone = ZoneId.of( TimeZones.map( TimeZones.map( zone.getId() ) ) ); this.value = value.withZoneSameInstant( mappedZone ); } this.epochSeconds = this.value.toEpochSecond(); }
/** * format date to string * * @param date date instance * @param pattern date format pattern * @return return string date */ public static String toString(Date date, String pattern) { Instant instant = new java.util.Date((date.getTime())).toInstant(); return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(pattern)); }
final Date date; if (value instanceof ZonedDateTime) { date = Date.from(((ZonedDateTime) value).toInstant()); } else if (value instanceof OffsetDateTime) { date = Date.from(((OffsetDateTime) value).toInstant()); } else if (value instanceof LocalDateTime) { final LocalDateTime localDateTime = (LocalDateTime) value; final ZoneId defaultZoneId = ZoneId.systemDefault(); final ZoneOffset offset = defaultZoneId.getRules().getOffset(localDateTime); date = Date.from(localDateTime.toInstant(offset)); } else if (value instanceof LocalDate) { final LocalDate localDate = (LocalDate) value; final LocalDateTime localDateTime = localDate.atStartOfDay(); final ZoneId defaultZoneId = ZoneId.systemDefault(); final ZoneOffset offset = defaultZoneId.getRules().getOffset(localDateTime); date = Date.from(localDateTime.toInstant(offset)); } else if (value instanceof Instant) { date = Date.from((Instant) value);
@Before public void before() { time = 1505892010L; date = new Date(1505892470110L); localDateTime = LocalDateTime.from(Instant.ofEpochSecond(time).atZone(ZoneId.systemDefault())); }
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(); } }
private Object convertDataTypeToDB(final Object params) { //https://jdbc.postgresql.org/documentation/head/8-date-time.html if (CommonConstant.DB_POSTGRESQL.equals(currentDBType) && params instanceof java.util.Date) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(((Date) params).getTime()), ZoneId.systemDefault()); } return params; }
public Date format(String date, String pattern) { DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, Locale.US); LocalDateTime formatted = LocalDateTime.parse(date, fmt); Instant instant = formatted.atZone(ZoneId.systemDefault()).toInstant(); return Date.from(instant); }
if (text.length() == 10 || text.length() == 8) { LocalDate localDate = parseLocalDate(text, format, formatter); localDateTime = LocalDateTime.of(localDate, LocalTime.MIN); } else { localDateTime = parseDateTime(text, formatter); LocalDate localDate; if (text.length() == 23) { LocalDateTime localDateTime = LocalDateTime.parse(text); localDate = LocalDate.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth()); } else { if (match) { Date date = s.getCalendar().getTime(); return (T) ZonedDateTime.ofInstant(date.toInstant(), timeZone.toZoneId()); ZoneId offsetTime = ZoneId.of(text); Instant instant = Instant.parse(text); return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId()); return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId()).toLocalDate(); return (T) ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId());
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
final ZonedDateTime zonedDateTime = value.atDate( LocalDate.of( 1970, 1, 1 ) ).atZone( ZoneId.systemDefault() ); final Instant instant = zonedDateTime.toInstant(); return (X) Date.from( instant ); return (X) Long.valueOf( instant.toEpochMilli() );
@Test public void lastModified() { HttpHeaders headers = new HttpHeaders(); ZonedDateTime lastModified = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")); headers.setLastModified(lastModified.toInstant().toEpochMilli()); HeaderAssertions assertions = headerAssertions(headers); assertions.lastModified(lastModified.toInstant().toEpochMilli()); try { assertions.lastModified(lastModified.toInstant().toEpochMilli() + 1); fail("Wrong value expected"); } catch (AssertionError error) { // Expected } }
private void assertTimestamp(String c4) { // '2014-09-08 17:51:04.777' // MySQL container is in UTC and the test time is during summer time period ZonedDateTime expectedTimestamp = ZonedDateTime.ofInstant( LocalDateTime.parse("2014-09-08T17:51:04.780").atZone(ZoneId.of("US/Samoa")).toInstant(), ZoneId.systemDefault()); ZoneId defaultZoneId = ZoneId.systemDefault(); ZonedDateTime c4DateTime = ZonedDateTime.parse(c4, ZonedTimestamp.FORMATTER).withZoneSameInstant(defaultZoneId); assertThat(c4DateTime.getYear()).isEqualTo(expectedTimestamp.getYear()); assertThat(c4DateTime.getMonth()).isEqualTo(expectedTimestamp.getMonth()); assertThat(c4DateTime.getDayOfMonth()).isEqualTo(expectedTimestamp.getDayOfMonth()); assertThat(c4DateTime.getHour()).isEqualTo(expectedTimestamp.getHour()); assertThat(c4DateTime.getMinute()).isEqualTo(expectedTimestamp.getMinute()); assertThat(c4DateTime.getSecond()).isEqualTo(expectedTimestamp.getSecond()); assertThat(c4DateTime.getNano()).isEqualTo(expectedTimestamp.getNano()); // We're running the connector in the same timezone as the server, so the timezone in the timestamp // should match our current offset ... LocalDateTime expectedLocalDateTime = LocalDateTime.parse("2014-09-08T17:51:04.780"); ZoneOffset expectedOffset = defaultZoneId.getRules().getOffset(expectedLocalDateTime); assertThat(c4DateTime.getOffset()).isEqualTo(expectedOffset); }
private static void raw(Exchange bfx) throws IOException { /* BitfinexTradeServiceRaw tradeService = (BitfinexTradeServiceRaw) bfx.getTradeService(); LimitOrder limitOrder = new LimitOrder.Builder(OrderType.BID, CurrencyPair.BTC_USD).limitPrice(new BigDecimal("481.69")) .originalAmount(new BigDecimal("0.001")).build(); tradeService.placeBitfinexLimitOrder(limitOrder, BitfinexOrderType.LIMIT); */ BitfinexTradeServiceRaw tradeService = (BitfinexTradeServiceRaw) bfx.getTradeService(); Date tenDaysAgo = Date.from(LocalDate.now().minusDays(10).atStartOfDay(ZoneId.systemDefault()).toInstant()); BitfinexFundingTradeResponse[] fundingTradeResponses = tradeService.getBitfinexFundingHistory("USD", tenDaysAgo, 2000); } }
@Test public void testConvertTimestamp() throws SQLException { LocalDateTime dateTime = LocalDateTime.of(2001, 5, 6, 12, 34, 56); Date sqlDate = Date.valueOf(dateTime.toLocalDate()); Time sqlTime = Time.valueOf(dateTime.toLocalTime()); Timestamp sqlTimestamp = Timestamp.valueOf(dateTime); java.util.Date javaDate = java.util.Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant()); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setTimestamp(i, sqlTimestamp)); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setObject(i, sqlTimestamp)); assertParameter(new Timestamp(sqlDate.getTime()), Types.TIMESTAMP, (ps, i) -> ps.setObject(i, sqlDate, Types.TIMESTAMP)); assertParameter(new Timestamp(sqlTime.getTime()), Types.TIMESTAMP, (ps, i) -> ps.setObject(i, sqlTime, Types.TIMESTAMP)); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setObject(i, sqlTimestamp, Types.TIMESTAMP)); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setObject(i, javaDate, Types.TIMESTAMP)); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setObject(i, dateTime, Types.TIMESTAMP)); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setObject(i, "2001-05-06 12:34:56", Types.TIMESTAMP)); }
/** * format string time to unix time * * @param time string date * @param pattern date format pattern * @return return unix time */ public static int toUnix(String time, String pattern) { LocalDateTime formatted = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(pattern)); return (int) formatted.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond(); }
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); } }
public static LocalDateTime fromMilliseconds(final long milliseconds) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), ZoneId.systemDefault()); }