java.time.LocalDateTime.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(242)

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

LocalDateTime.<init>介绍

[英]Constructor.
[中]构造器。

代码示例

代码示例来源:origin: stackoverflow.com

LocalDate localDate = new LocalDate();
DateTime dateTime = new DateTime();
LocalDateTime localDateTime = new LocalDateTime();
DateTimeZone dateTimeZone = DateTimeZone.getDefault();

代码示例来源:origin: stackoverflow.com

LocalDate startDate = new LocalDate(2014, 1, 2);
LocalDateTime startDateTime = new LocalDateTime(2014, 1, 2, 14, 0);
LocalDate forCompare = startDateTime.toLocalDate();
System.out.println("equal dates: " + forCompare.equals(startDate));
// equal dates: true

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Returns a copy of this date-time with the new date and time, checking
 * to see if a new object is in fact required.
 *
 * @param newDate  the date of the new date-time, not null
 * @param newTime  the time of the new date-time, not null
 * @return the date-time, not null
 */
private LocalDateTime with(LocalDate newDate, LocalTime newTime) {
  if (date == newDate && time == newTime) {
    return this;
  }
  return new LocalDateTime(newDate, newTime);
}

代码示例来源:origin: stackoverflow.com

class COMDateToRegularDateConverter {
  private static final LocalDateTime ZERO_COM_TIME = new LocalDateTime(1899, 12, 30, 0, 0);
  private static final BigDecimal MILLIS_PER_DAY = new BigDecimal(86400000);

  LocalDateTime toLocalDateTime(BigDecimal comTime) {
    BigDecimal daysAfterZero = comTime.setScale(0, RoundingMode.DOWN);
    BigDecimal fraction = comTime.subtract(daysAfterZero).abs(); //fraction always represents the time of that day
    BigDecimal fractionMillisAfterZero = fraction.multiply(MILLIS_PER_DAY).setScale(0, RoundingMode.HALF_DOWN);

    return ZERO_COM_TIME.plusDays(daysAfterZero.intValue()).plusMillis(fractionMillisAfterZero.intValue());
  }
}

代码示例来源:origin: stackoverflow.com

LocalDateTime ldt1 = new LocalDateTime(2014, 5, 1, 0, 0, 0);
LocalDateTime ldt2 = new LocalDateTime(2014, 5, 2, 23, 59, 59);

int days = Days.daysBetween(ldt1.toLocalDate(), ldt2.toLocalDate()).getDays();
if (ldt1.toLocalTime().equals(new LocalTime(0, 0))) {
 days++;
}

代码示例来源:origin: stackoverflow.com

LocalDateTime tsp1 = new LocalDateTime(); // now as example
LocalDateTime tsp2 = tsp1.plusMinutes(minutes);
Period p = new Period(tsp1, tsp2, PeriodType.standard()); // or other period type?

代码示例来源:origin: stackoverflow.com

LocalDateTime ldt = new LocalDateTime();
ldt = ldt.minusDays(30);

代码示例来源:origin: stackoverflow.com

LocalDateTime localDateTime = new LocalDateTime("2010-10-22");
System.out.println("Formatted time: " + localDateTime.toString());

代码示例来源:origin: stackoverflow.com

Duration dur = new Duration(5000001 * 1000L); // in milliseconds
LocalDateTime ref = new LocalDateTime(); // reference timestamp
LocalDateTime end = ref.plus(dur);

// construct normalized duration
PeriodType type = PeriodType.yearMonthDayTime().withMillisRemoved();
Period p = new Period(ref, end, type);

// print the new normalized duration
System.out.println(p); // P1M26DT20H53M21S

代码示例来源:origin: stackoverflow.com

DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
LocalDateTime ldt = new LocalDateTime();
String val = ldt.toString(formatter);
System.out.println(val);    
val += "Z";
LocalDateTime nldt = LocalDateTime.parse(val, formatter);
System.out.println(nldt.toString());

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Obtains an instance of {@code LocalDateTime} from year, month,
 * day, hour and minute, setting the second and nanosecond to zero.
 * <p>
 * The day must be valid for the year and month, otherwise an exception will be thrown.
 * The second and nanosecond fields will be set to zero.
 *
 * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 * @param hour  the hour-of-day to represent, from 0 to 23
 * @param minute  the minute-of-hour to represent, from 0 to 59
 * @return the local date-time, not null
 * @throws DateTimeException if the value of any field is out of range
 * @throws DateTimeException if the day-of-month is invalid for the month-year
 */
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) {
  LocalDate date = LocalDate.of(year, month, dayOfMonth);
  LocalTime time = LocalTime.of(hour, minute);
  return new LocalDateTime(date, time);
}

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Obtains an instance of {@code LocalDateTime} from year, month,
 * day, hour and minute, setting the second and nanosecond to zero.
 * <p>
 * The day must be valid for the year and month, otherwise an exception will be thrown.
 * The second and nanosecond fields will be set to zero.
 *
 * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 * @param month  the month-of-year to represent, not null
 * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 * @param hour  the hour-of-day to represent, from 0 to 23
 * @param minute  the minute-of-hour to represent, from 0 to 59
 * @return the local date-time, not null
 * @throws DateTimeException if the value of any field is out of range
 * @throws DateTimeException if the day-of-month is invalid for the month-year
 */
public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) {
  LocalDate date = LocalDate.of(year, month, dayOfMonth);
  LocalTime time = LocalTime.of(hour, minute);
  return new LocalDateTime(date, time);
}

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Obtains an instance of {@code LocalDateTime} from year, month,
 * day, hour, minute, second and nanosecond.
 * <p>
 * The day must be valid for the year and month, otherwise an exception will be thrown.
 *
 * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 * @param month  the month-of-year to represent, not null
 * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 * @param hour  the hour-of-day to represent, from 0 to 23
 * @param minute  the minute-of-hour to represent, from 0 to 59
 * @param second  the second-of-minute to represent, from 0 to 59
 * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
 * @return the local date-time, not null
 * @throws DateTimeException if the value of any field is out of range
 * @throws DateTimeException if the day-of-month is invalid for the month-year
 */
public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
  LocalDate date = LocalDate.of(year, month, dayOfMonth);
  LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);
  return new LocalDateTime(date, time);
}

代码示例来源:origin: stackoverflow.com

LocalDate startDate = new LocalDate(2014, 1, 2);
LocalDateTime startDateTime = new LocalDateTime(2014, 1, 2, 14, 0);
LocalDate forCompare = startDateTime.toLocalDate();
System.out.println("equal dates: " + forCompare.equals(startDate));

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Obtains an instance of {@code LocalDateTime} from a date and time.
 *
 * @param date  the local date, not null
 * @param time  the local time, not null
 * @return the local date-time, not null
 */
public static LocalDateTime of(LocalDate date, LocalTime time) {
  Jdk8Methods.requireNonNull(date, "date");
  Jdk8Methods.requireNonNull(time, "time");
  return new LocalDateTime(date, time);
}

代码示例来源:origin: stackoverflow.com

LocalDateTime now = new LocalDateTime();
LocalDateTime then = now.minusDays(2).minusMinutes(5);
int numberOfDaysBetween = Days.daysBetween(then, now).getDays();
LocalDateTime fullDayTime = then.plusDays(numberOfDaysBetween);
if (fullDayTime.isBefore(now)) {
  numberOfDaysBetween++;
}

代码示例来源:origin: stackoverflow.com

LocalDateTime ldt = new LocalDateTime( 1975, 4, 8, 15, 16, 17, 18 );        
DateTime dt = new DateTime( ldt.getYear(), ldt.getMonthOfYear(), ldt.getDayOfMonth(), ldt.getHourOfDay(), ldt.getMinuteOfHour(), ldt.getSecondOfMinute(), ldt.getMillisOfSecond());
Date d = new Date( dt.getMillis() );

代码示例来源:origin: stackoverflow.com

final LocalDateTime now = new LocalDateTime().withSecondOfMinute(0).withMillisOfDay(0);
final LocalDateTime tenThirty;
if (now.getHourOfDay() <= 10 && now.getMinuteOfHour() <= 30) {
  tenThirty = now.withHourOfDay(10).withMinuteOfHour(30);
} else {
  tenThirty = now.plusDays(1).withHourOfDay(10).withMinuteOfHour(30);
}

代码示例来源:origin: stackoverflow.com

LocalDateTime now = new LocalDateTime(); // automatically points to current datetime.
int hour = now.getHourOfDay();
int day = now.getDayOfMonth();
int week = now.getWeekOfWeekyear();
int month = now.getMonthOfYear();
int year = now.getYear();
int season = getSeason(day, month);

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Obtains an instance of {@code LocalDateTime} using seconds from the
 * epoch of 1970-01-01T00:00:00Z.
 * <p>
 * This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field
 * to be converted to a local date-time. This is primarily intended for
 * low-level conversions rather than general application usage.
 *
 * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
 * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
 * @param offset  the zone offset, not null
 * @return the local date-time, not null
 * @throws DateTimeException if the result exceeds the supported range
 */
public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) {
  Jdk8Methods.requireNonNull(offset, "offset");
  long localSecond = epochSecond + offset.getTotalSeconds();  // overflow caught later
  long localEpochDay = Jdk8Methods.floorDiv(localSecond, SECONDS_PER_DAY);
  int secsOfDay = Jdk8Methods.floorMod(localSecond, SECONDS_PER_DAY);
  LocalDate date = LocalDate.ofEpochDay(localEpochDay);
  LocalTime time = LocalTime.ofSecondOfDay(secsOfDay, nanoOfSecond);
  return new LocalDateTime(date, time);
}

相关文章

LocalDateTime类方法