java.time.LocalDateTime类的使用及代码示例

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

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

LocalDateTime介绍

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

代码示例

代码示例来源:origin: yu199195/hmily

private Date acquireData() {
  return new Date(LocalDateTime.now().atZone(ZoneId.systemDefault())
      .toInstant().toEpochMilli() - (hmilyConfig.getRecoverDelayTime() * 1000));
}

代码示例来源:origin: lets-blade/blade

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

代码示例来源:origin: lets-blade/blade

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

代码示例来源:origin: alibaba/Sentinel

@Override
  public String doAnother() {
    return LocalDateTime.now().toString();
  }
}

代码示例来源:origin: baomidou/mybatis-plus

/**
 * 格式化的毫秒时间
 */
public static String getMillisecond() {
  return LocalDateTime.now().format(MILLISECOND);
}

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

LocalDateTime now = LocalDateTime.now();
String format1 = now.format(DateTimeFormatter.ISO_DATE_TIME);
String format2 = now.atZone(ZoneId.of("GMT")).format(DateTimeFormatter.RFC_1123_DATE_TIME);
String format3 = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss", Locale.ENGLISH));

System.out.println(format1);
System.out.println(format2);
System.out.println(format3);

代码示例来源:origin: prestodb/presto

@Test
public void testConvertTime()
    throws SQLException
{
  LocalTime time = LocalTime.of(12, 34, 56);
  Time sqlTime = Time.valueOf(time);
  java.util.Date javaDate = new java.util.Date(sqlTime.getTime());
  LocalDateTime dateTime = LocalDateTime.of(LocalDate.of(2001, 5, 6), time);
  Timestamp sqlTimestamp = Timestamp.valueOf(dateTime);
  assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setTime(i, sqlTime));
  assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, sqlTime));
  assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, sqlTime, Types.TIME));
  assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, sqlTimestamp, Types.TIME));
  assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, javaDate, Types.TIME));
  assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, dateTime, Types.TIME));
  assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, "12:34:56", Types.TIME));
}

代码示例来源:origin: nutzam/nutz

@Override
public TemporalAccessor cast(Number src, Class<?> toType, String... args) {
  Date date = new Date(src.longValue());
  LocalDateTime dt = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
  if (toType == LocalDateTime.class)
    return dt;
  if (toType == LocalDate.class)
    return dt.toLocalDate();
  return dt.toLocalTime();
}

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

Date in = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault());
Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());

代码示例来源:origin: lets-blade/blade

@Test
public void testLocal(){
  Map<String,Object> result = new HashMap<>(8);
  result.put("date1", new Date());
  result.put("date2", LocalDate.now());
  result.put("date3", LocalDateTime.now());
  System.out.println(JsonKit.toString(result));
}

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

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

代码示例来源:origin: twosigma/beakerx

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

代码示例来源:origin: yu199195/hmily

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

代码示例来源:origin: hibernate/hibernate-orm

/**
   * Set the transient property at load time based on a calculation.
   * Note that a native Hibernate formula mapping is better for this purpose.
   */
  @PostLoad
  public void calculateAge() {
    age = ChronoUnit.YEARS.between( LocalDateTime.ofInstant(
        Instant.ofEpochMilli( dateOfBirth.getTime()), ZoneOffset.UTC),
      LocalDateTime.now()
    );
  }
}

代码示例来源:origin: alibaba/fastjson

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 {
  localDate = parseLocalDate(text, format, formatter);
LocalTime localDate;
if (text.length() == 23) {
  LocalDateTime localDateTime = LocalDateTime.parse(text);
  localDate = LocalTime.of(localDateTime.getHour(), localDateTime.getMinute(),
      localDateTime.getSecond(), localDateTime.getNano());
} else {
  localDate = LocalTime.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) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId()).toLocalTime();

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: lets-blade/blade

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

代码示例来源:origin: oblac/jodd

public static LocalDateTime fromMilliseconds(final long milliseconds) {
  return LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), ZoneId.systemDefault());
}

代码示例来源:origin: prestodb/presto

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

代码示例来源:origin: prestodb/presto

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

相关文章

LocalDateTime类方法