/** * Combines this time with a date to create a {@code LocalDateTime}. * <p> * This returns a {@code LocalDateTime} formed from this time at the specified date. * All possible combinations of date and time are valid. * * @param date the date to combine with, not null * @return the local date-time formed from this time and the specified date, not null */ public LocalDateTime atDate(LocalDate date) { return LocalDateTime.of(date, this); }
/** * Obtains an instance of {@code LocalDateTime} from a text string such as {@code 2007-12-23T10:15:30}. * <p> * The string must represent a valid date-time and is parsed using * {@link org.threeten.bp.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME}. * * @param text the text to parse such as "2007-12-23T10:15:30", not null * @return the parsed local date-time, not null * @throws DateTimeParseException if the text cannot be parsed */ public static LocalDateTime parse(CharSequence text) { return parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME); }
/** * Converts a {@code LocalDateTime} to a {@code java.sql.Timestamp}. * * @param dateTime the local date-time, not null * @return the SQL timestamp, not null */ @SuppressWarnings("deprecation") public static Timestamp toSqlTimestamp(LocalDateTime dateTime) { return new Timestamp( dateTime.getYear() - 1900, dateTime.getMonthValue() - 1, dateTime.getDayOfMonth(), dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(), dateTime.getNano()); }
@Nonnull @Override public LocalTime convert(Date source) { return ofInstant(ofEpochMilli(source.getTime()), systemDefault()).toLocalTime(); } }
@Nonnull @Override public LocalDate convert(Date source) { return ofInstant(ofEpochMilli(source.getTime()), systemDefault()).toLocalDate(); } }
@Override public void serialize(LocalDateTime value, JsonGenerator g, SerializerProvider provider) throws IOException { if (useTimestamp(provider)) { g.writeStartArray(); g.writeNumber(value.getYear()); g.writeNumber(value.getMonthValue()); g.writeNumber(value.getDayOfMonth()); g.writeNumber(value.getHour()); g.writeNumber(value.getMinute()); if (value.getSecond() > 0 || value.getNano() > 0) { g.writeNumber(value.getSecond()); if(value.getNano() > 0) { if (provider.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)) g.writeNumber(value.getNano()); else g.writeNumber(value.get(ChronoField.MILLI_OF_SECOND)); } } g.writeEndArray(); } else { DateTimeFormatter dtf = _formatter; if (dtf == null) { dtf = _defaultFormatter(); } g.writeString(value.format(dtf)); } }
private Duration getDifferenceBetweenTimeAndNow(long timeStart) { LocalDateTime today = LocalDateTime.now(); LocalDateTime otherTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStart), ZoneId.systemDefault()); return Duration.between(otherTime, today); }
@Nonnull @Override public LocalDateTime convert(java.time.Instant source) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(source.toEpochMilli()), ZoneOffset.systemDefault()); } }
return resolveLocal(LocalDateTime.of((LocalDate) adjuster, dateTime.toLocalTime())); } else if (adjuster instanceof LocalTime) { return resolveLocal(LocalDateTime.of(dateTime.toLocalDate(), (LocalTime) adjuster)); } else if (adjuster instanceof LocalDateTime) { return resolveLocal((LocalDateTime) adjuster); } else if (adjuster instanceof Instant) { Instant instant = (Instant) adjuster; return create(instant.getEpochSecond(), instant.getNano(), zone); } else if (adjuster instanceof ZoneOffset) { return resolveOffset((ZoneOffset) adjuster);
/** Returns TAI milliseconds mod 2^32 for the given date. * * Since java int is signed 32 bit integer, return long instead. * It is the same on byte level, but just to avoid confusing people with negative values here. * * * From http://stjarnhimlen.se/comp/time.html: * * TAI (Temps Atomique International or International Atomic Time) is * defined as the weighted average of the time kept by about 200 * atomic clocks in over 50 national laboratories worldwide. * TAI-UT1 was approximately 0 on 1958 Jan 1. * (TAI is ahead of UTC by 35 seconds as of 2014.) * * GPS time = TAI - 19 seconds. GPS time matched UTC from 1980-01-01 * to 1981-07-01. No leap seconds are inserted into GPS time, thus * GPS time is 13 seconds ahead of UTC on 2000-01-01. The GPS epoch * is 00:00 (midnight) UTC on 1980-01-06. * The difference between GPS Time and UTC changes in increments of * seconds each time a leap second is added to UTC time scale. */ public static long instantToTaiMillisSince2004Mod32(Instant instantX) { OffsetDateTime gnEpochStart = OffsetDateTime.of(LocalDateTime.of(2004, Month.JANUARY, 1, 0, 0), ZoneOffset.UTC); long millis2004 = gnEpochStart.toInstant().toEpochMilli(); long millisAtX = instantX.toEpochMilli(); long taiMillis = (millisAtX + LEAP_SECONDS_SINCE_2004*1000) - millis2004; return taiMillis % (1L << 32); }
startDate = zoneOffsetTransition.getDateTimeBefore(); } else { startDate = LocalDateTime.now(zoneId); int transitionRuleMonthValue = transitionRule.getMonth().getValue(); DayOfWeek transitionRuleDayOfWeek = transitionRule.getDayOfWeek(); LocalDateTime ldt = LocalDateTime.now(zoneId) .with(TemporalAdjusters.firstInMonth(transitionRuleDayOfWeek)) .withMonth(transitionRuleMonthValue) .with(transitionRule.getLocalTime()); Month month = ldt.getMonth(); allDaysOfWeek.add(ldt.getDayOfMonth()); }while((ldt = ldt.plus(org.threeten.bp.Period.ofWeeks(1))).getMonth() == month); observance.getProperties().add(offsetTo); observance.getProperties().add(rrule); observance.getProperties().add(new DtStart(String.format(DATE_TIME_TPL, startDate.withMonth(transitionRule.getMonth().getValue()) .withDayOfMonth(transitionRule.getDayOfMonthIndicator()) .with(transitionRule.getDayOfWeek()))));
/** * Adds a single transition rule to the current window. * <p> * This adds a rule such that the offset, expressed as a daylight savings amount, * changes at the specified date-time. * * @param transitionDateTime the date-time that the transition occurs as defined by timeDefintion, not null * @param timeDefinition the definition of how to convert local to actual time, not null * @param savingAmountSecs the amount of saving from the standard offset after the transition in seconds * @return this, for chaining * @throws IllegalStateException if no window has yet been added * @throws IllegalStateException if the window already has fixed savings * @throws IllegalStateException if the window has reached the maximum capacity of 2000 rules */ public ZoneRulesBuilder addRuleToWindow( LocalDateTime transitionDateTime, TimeDefinition timeDefinition, int savingAmountSecs) { Jdk8Methods.requireNonNull(transitionDateTime, "transitionDateTime"); return addRuleToWindow( transitionDateTime.getYear(), transitionDateTime.getYear(), transitionDateTime.getMonth(), transitionDateTime.getDayOfMonth(), null, transitionDateTime.toLocalTime(), false, timeDefinition, savingAmountSecs); }
LocalDateTime loopWindowStart = deduplicate(LocalDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0)); ZoneOffset loopWindowOffset = firstWallOffset; window.tidy(loopWindowStart.getYear()); for (TZRule rule : window.ruleList) { ZoneOffsetTransition trans = rule.toTransition(loopStandardOffset, loopSavings); if (trans.toEpochSecond() > loopWindowStart.toEpochSecond(loopWindowOffset)) { standardTransitionList.add(deduplicate( new ZoneOffsetTransition( LocalDateTime.ofEpochSecond(loopWindowStart.toEpochSecond(loopWindowOffset), 0, loopStandardOffset), loopStandardOffset, window.standardOffset))); loopStandardOffset = deduplicate(window.standardOffset); if (trans.toEpochSecond() < loopWindowStart.toEpochSecond(loopWindowOffset) == false && trans.toEpochSecond() < window.createDateTimeEpochSecond(loopSavings) && trans.getOffsetBefore().equals(trans.getOffsetAfter()) == false) { loopWindowStart = deduplicate(LocalDateTime.ofEpochSecond( window.createDateTimeEpochSecond(loopSavings), 0, loopWindowOffset));
public static LocalDateTime deserializeLocalDateTime(final JsonReader reader) throws IOException { final char[] tmp = reader.readSimpleQuote(); final int len = reader.getCurrentIndex() - reader.getTokenStart() - 1; if (len > 18 && len < 30 && tmp[4] == '-' && tmp[7] == '-' && (tmp[10] == 'T' || tmp[10] == 't' || tmp[10] == ' ') && tmp[13] == ':' && tmp[16] == ':' && allDigits(tmp, 20, len)) { final int year = NumberConverter.read4(tmp, 0); final int month = NumberConverter.read2(tmp, 5); final int day = NumberConverter.read2(tmp, 8); final int hour = NumberConverter.read2(tmp, 11); final int min = NumberConverter.read2(tmp, 14); final int sec = NumberConverter.read2(tmp, 17); if (len > 19 && tmp[19] == '.') { final int nanos = readNanos(tmp, len); return LocalDateTime.of(year, month, day, hour, min, sec, nanos); } return LocalDateTime.of(year, month, day, hour, min, sec); } else { return LocalDateTime.parse(new String(tmp, 0, len)); } }
LocalDateTime ldt = LocalDateTime.of(year, month, day, hour, min, sec, 0).plusDays(days); instantSecs = ldt.toEpochSecond(ZoneOffset.UTC); instantSecs += Jdk8Methods.safeMultiply(yearParsed / 10000L, SECONDS_PER_10000_YEARS); } catch (RuntimeException ex) {
long hi = Jdk8Methods.floorDiv(zeroSecs, SECONDS_PER_10000_YEARS) + 1; long lo = Jdk8Methods.floorMod(zeroSecs, SECONDS_PER_10000_YEARS); LocalDateTime ldt = LocalDateTime.ofEpochSecond(lo - SECONDS_0000_TO_1970, 0, ZoneOffset.UTC); if (hi > 0) { buf.append('+').append(hi); if (ldt.getSecond() == 0) { buf.append(":00"); long hi = zeroSecs / SECONDS_PER_10000_YEARS; long lo = zeroSecs % SECONDS_PER_10000_YEARS; LocalDateTime ldt = LocalDateTime.ofEpochSecond(lo - SECONDS_0000_TO_1970, 0, ZoneOffset.UTC); int pos = buf.length(); buf.append(ldt); if (ldt.getSecond() == 0) { buf.append(":00"); if (ldt.getYear() == -10000) { buf.replace(pos, pos + 2, Long.toString(hi - 1)); } else if (lo == 0) {
return LocalDateTime.ofInstant(Instant.parse(string), ZoneOffset.UTC).toLocalDate(); } else { return LocalDate.parse(string, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
static LocalDateTime readExternal(DataInput in) throws IOException { LocalDate date = LocalDate.readExternal(in); LocalTime time = LocalTime.readExternal(in); return LocalDateTime.of(date, time); }
@Before public void setUp() throws Exception { jsr301DateTime = LocalDateTime.of(1970, Month.MAY, 18, 13, 30, 0, 20000000); jsr301Date = LocalDate.of(1970, Month.MAY, 18); jsr301Time = LocalTime.of(13, 30, 0); jodaDateTime = new DateTime(1970, 5, 18, 13, 30, 0, 20); jodaDateTimeUtc = new DateTime(1970, 5, 18, 13, 30, 0, 20, DateTimeZone.UTC); }
/** * Gets the day-of-month field. * <p> * This method returns the primitive {@code int} value for the day-of-month. * * @return the day-of-month, from 1 to 31 */ public int getDayOfMonth() { return dateTime.getDayOfMonth(); }