Refine search
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); }
static String formatDate(long date) { Instant instant = Instant.ofEpochMilli(date); ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT); return DATE_FORMATTERS[0].format(time); }
@Override public ZonedDateTime createdAt() { return ZonedDateTime.parse("2016-11-16T17:21:00Z"); }
private String prettyResult(Object entryValue, String tzID) { if (entryValue instanceof InetAddress ) { return ((InetAddress) entryValue).getHostAddress(); } else if (entryValue instanceof Date) { ZonedDateTime zdt = ZonedDateTime.from(((Date) entryValue).toInstant().atZone(ZoneId.of(tzID))); return(String.valueOf(zdt.format(dateTimeFormatter))); } else { return String.valueOf(entryValue); } }
@Override OffsetTime getTimePart( Supplier<ZoneId> defaultZone ) { ZoneOffset currentOffset = assertValidArgument( () -> ZonedDateTime.ofInstant( Instant.now(), defaultZone.get() ) ).getOffset(); return OffsetTime.of(value.toLocalTime(), currentOffset); }
private Date acquireData() { return new Date(LocalDateTime.now().atZone(ZoneId.systemDefault()) .toInstant().toEpochMilli() - (hmilyConfig.getRecoverDelayTime() * 1000)); }
Date input = new Date(); Instant instant = input.toInstant(); ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault()); LocalDate date = zdt.toLocalDate();
public void setModel(GCModel model, GCPreferences preferences) { this.model = model; if (model.getFirstDateStamp() != null) { this.timeOffsetPanel.setDate(new Date(model.getFirstDateStamp().toInstant().toEpochMilli())); } applyPreferences(preferences); setScaleFactor(getScaleFactor()); setRunningTime(model.getRunningTime()); setFootprint(model.getFootprint()); setMaxPause(model.getPause().getMax()); }
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 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"); } }
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
@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 } }
/** * 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); }
/** * 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(); }
/** * Converts local date time to epoh milliseconds. */ public static long toMilliseconds(final LocalDateTime localDateTime) { return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); }
@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)); }
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); } }
public static SqlTime sqlTimeOf(LocalTime time, Session session) { if (session.toConnectorSession().isLegacyTimestamp()) { long millisUtc = LocalDate.ofEpochDay(0) .atTime(time) .atZone(UTC) .withZoneSameLocal(ZoneId.of(session.getTimeZoneKey().getId())) .toInstant() .toEpochMilli(); return new SqlTime(millisUtc, session.getTimeZoneKey()); } return new SqlTime(NANOSECONDS.toMillis(time.toNanoOfDay())); }
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); }