LocalDateTime类

所在包java.time.LocalDateTime

jdk1.8以上支持

static LocalDateTime now()
用法

LocalDateTime now = LocalDateTime.now();
System.out.println(localDateTime);  // 字符串格式为2019-04-11T16:14:34.338

static LocalDateTime parse(CharSequence text)
从一个文本字符串(如 2007-12-03T10:15:30获取一个 LocalDateTime的实例。

LocalDateTime.parse("2007-12-03T10:15:30")

相当于toString()的逆过程。只能解析date+‘T’+time格式的CharSequence对象。

toString()源码:

public String toString() {
        return date.toString() + 'T' + time.toString();
    }

LocalDateTime把日期和时间分开处理,定义两个final类型的成员变量date和time

源码:

/**
 * The date part.
 */
private final LocalDate date;
/**
 * The time part.
 */
private final LocalTime time;

LocalDateTime plusDays(long days)
返回此 LocalDateTime的副本,并以指定的时间段添加天数。

LocalDateTime now = LocalDateTime.now();
LocalDateTime startDate = now. plusDays(1);

类似的还有plusHours() 、plusMinutes()、plusMonths() 、plusNanos()、plusSeconds()、plusWeeks()、plusYears()
还有对应的减少时间的函数minusHours()、minusMonths()等

LocalDate withDayOfMonth(int dayOfMonth)
更改此时间的日期,返回新的对象。

LocalDateTime now = LocalDateTime.now();
LocalDateTime startDate = now.withDayOfMonth(2);

类似方法withDayOfYear(int dayOfYear) ,withHour(int hour) ,withMinute(int minute) ,withMonth(int month) ,withNano(int nanoOfSecond) ,withSecond(int second) ,withYear(int year)

static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
从年,月,日,小时和分钟获得 LocalDateTime的实例,将秒和纳秒设置为零。
LocalDateTime time = LocalDateTime.of(1999,11,22,4,23);
参数最全的:
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)

static LocalDateTim of(LocalDate date, LocalTime time)
从日期和时间获取 LocalDateTime一个实例。

static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset)
使用从1970-01-01T00:00:00Z的时代开始的秒数获得一个 LocalDateTime的实例。

int getDayOfMonth()
获取月份字段。
getDayOfWeek() ,getDayOfYear() ,getHour() ,getMinute() 等

boolean isAfter(ChronoLocalDateTime<?> other)
检查这个日期时间是否在指定的日期之后。
isBefore()、isEqual()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值