org.threeten.bp.LocalDateTime类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(12.3k)|赞(0)|评价(0)|浏览(249)

本文整理了Java中org.threeten.bp.LocalDateTime类的一些代码示例,展示了LocalDateTime类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LocalDateTime类的具体详情如下:
包路径:org.threeten.bp.LocalDateTime
类名称:LocalDateTime

LocalDateTime介绍

[英]A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-23T10:15:30.

LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond precision. For example, the value "2nd October 2007 at 13:45.30.123456789" can be stored in a LocalDateTime.

This class does not store or represent a time-zone. Instead, it is a description of the date, as used for birthdays, combined with the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. It is equivalent to the proleptic Gregorian calendar system, in which today's rules for leap years are applied for all time. For most applications written today, the ISO-8601 rules are entirely suitable. However, any application that makes use of historical dates, and requires them to be accurate will find the ISO-8601 approach unsuitable.

Specification for implementors

This class is immutable and thread-safe.
[中]ISO-8601日历系统中没有时区的日期时间,例如2007-12-23T10:15:30。
LocalDateTime是一个不可变的日期时间对象,它表示一个日期时间,通常被视为年-月-日-小时-分-秒。还可以访问其他日期和时间字段,例如年中的日期、周中的日期和周。时间表示为纳秒精度。例如,值“2007年10月2日13:45.30.123456789”可以存储在LocalDateTime中。
此类不存储或表示时区。相反,它是对日期的描述,用于生日,与挂钟上显示的当地时间相结合。如果没有诸如偏移量或时区之类的附加信息,它不能表示时间线上的某个瞬间。
ISO-8601日历系统是当今世界大部分地区使用的现代民用日历系统。它相当于公历的前身,即今天的闰年规则在任何时候都适用。对于今天编写的大多数应用程序,ISO-8601规则完全适用。然而,任何使用历史日期并要求其准确的应用程序都会发现ISO-8601方法不合适。
####实施者规范
这个类是不可变的,并且是线程安全的。

代码示例

代码示例来源:origin: ThreeTen/threetenbp

/**
 * 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);
}

代码示例来源:origin: ThreeTen/threetenbp

/**
 * 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);
}

代码示例来源:origin: ThreeTen/threetenbp

/**
 * 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());
}

代码示例来源:origin: apache/servicemix-bundles

@Nonnull
  @Override
  public LocalTime convert(Date source) {
    return ofInstant(ofEpochMilli(source.getTime()), systemDefault()).toLocalTime();
  }
}

代码示例来源:origin: apache/servicemix-bundles

@Nonnull
  @Override
  public LocalDate convert(Date source) {
    return ofInstant(ofEpochMilli(source.getTime()), systemDefault()).toLocalDate();
  }
}

代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp

@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));
  }
}

代码示例来源:origin: riggaroo/android-things-electricity-monitor

private Duration getDifferenceBetweenTimeAndNow(long timeStart) {
  LocalDateTime today = LocalDateTime.now();
  LocalDateTime otherTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStart), ZoneId.systemDefault());
  return Duration.between(otherTime, today);
}

代码示例来源:origin: apache/servicemix-bundles

@Nonnull
  @Override
  public LocalDateTime convert(java.time.Instant source) {
    return LocalDateTime.ofInstant(Instant.ofEpochMilli(source.toEpochMilli()), ZoneOffset.systemDefault());
  }
}

代码示例来源:origin: ThreeTen/threetenbp

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);

代码示例来源:origin: alexvoronov/geonetworking

/** 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);
}

代码示例来源:origin: net.oneandone.ical4j/ical4j

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()))));

代码示例来源:origin: ThreeTen/threetenbp

/**
 * 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);
}

代码示例来源:origin: ThreeTen/threetenbp

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));

代码示例来源:origin: ngs-doo/dsl-json

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));
  }
}

代码示例来源:origin: ThreeTen/threetenbp

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) {

代码示例来源:origin: org.threeten/threetenbp

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) {

代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp

return LocalDateTime.ofInstant(Instant.parse(string), ZoneOffset.UTC).toLocalDate();
} else {
  return LocalDate.parse(string, DateTimeFormatter.ISO_LOCAL_DATE_TIME);

代码示例来源:origin: ThreeTen/threetenbp

static LocalDateTime readExternal(DataInput in) throws IOException {
  LocalDate date = LocalDate.readExternal(in);
  LocalTime time = LocalTime.readExternal(in);
  return LocalDateTime.of(date, time);
}

代码示例来源:origin: jeffdcamp/dbtools-android

@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);
}

代码示例来源:origin: ThreeTen/threetenbp

/**
 * 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();
}

相关文章