时间类(五)【LocalDateTime】

五、LocalDateTime类

java.time.LocalDateTime 是一个不可变的日期时间对象,代表日期时间。

public final class LocalDateTime
implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>,
Serializable {

1.字段

在 java.time.LocalDateTime 类中声明了两个实例字段用来存储 日期 和 时间 :

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

可以看出其实 LocalDateTime 底层就是一个 LocalDate + LocalTime 。

这里需要注意,不仅仅 date 和 time 字段是 final 修饰的, LocalDate 、 LocalTime类中的实例字段也是 final 修饰的,因此 LocalDateTime 的实例一经创建,其内部的各项取值都是不可更改的。

2.获取LocalDateTime实例

public static LocalDateTime now();
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int
minute);
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int
minute, int second);
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int
minute, int second, int nanoOfSecond);
public static LocalDateTime of(LocalDate date, LocalTime time);

示例:

    public static void main(String[] args) {
        LocalDateTime datetime = LocalDateTime.now();
        System.out.println(datetime);
        
        LocalDateTime first = LocalDateTime.of(1995, 7, 4, 7, 30);
        System.out.println(first);//1995-07-04T07:30

        LocalDateTime third = LocalDateTime.of(1999, 11, 20, 6, 30, 7, 0);
        System.out.println(third);//1999-11-20T06:30:07
        
        //LocalDate\LocalTime 转换成LocalDateTime
        LocalDate date = LocalDate.of(1997, 7, 1);
        LocalTime time = LocalTime.of(10, 20);
        LocalDateTime fourth = LocalDateTime.of(date, time);
        System.out.println(fourth);//1997-07-01T10:20
    }

3.方法

now()LocalDateTime返回当前日期和时间。
of(int year, int month, int dayOfMonth, int hour, int minute)LocalDateTime使用给定的年、月、日、小时和分钟创建一个 LocalDateTime 对象。
of(int year, int month, int dayOfMonth, int hour, int minute, int second)LocalDateTime使用给定的年、月、日、小时、分钟和秒创建一个 LocalDateTime 对象。
of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)LocalDateTime使用给定的年、月、日、小时、分钟、秒和纳秒创建一个 LocalDateTime 对象。
of(LocalDate date, LocalTime time)LocalDateTime使用给定的日期和时间创建一个 LocalDateTime 对象。
parse(CharSequence text)LocalDateTime从字符串解析日期和时间。
getYear()int获取年份。
getMonth()Month获取月份。
getDayOfMonth()int获取月份中的天数。
getDayOfWeek()DayOfWeek获取星期几。
getDayOfYear()int获取一年中的天数。
getHour()int获取小时。
getMinute()int获取分钟。
getSecond()int获取秒数。
getNano()int获取纳秒数。
withYear(int year)LocalDateTime设置年份并返回一个新的 LocalDateTime 对象。
withMonth(int month)LocalDateTime设置月份并返回一个新的 LocalDateTime 对象。
withDayOfMonth(int dayOfMonth)LocalDateTime设置月份中的天数并返回一个新的 LocalDateTime 对象。
withDayOfYear(int dayOfYear)LocalDateTime设置一年中的天数并返回一个新的 LocalDateTime 对象。
withHour(int hour)LocalDateTime设置小时并返回一个新的 LocalDateTime 对象。
withMinute(int minute)LocalDateTime设置分钟并返回一个新的 LocalDateTime 对象。
withSecond(int second)LocalDateTime设置秒数并返回一个新的 LocalDateTime 对象。
withNano(int nanoOfSecond)LocalDateTime设置纳秒数并返回一个新的 LocalDateTime 对象。
plusYears(long yearsToAdd)LocalDateTime将指定的年数添加到日期时间并返回一个新的 LocalDateTime 对象。
plusMonths(long monthsToAdd)LocalDateTime将指定的月数添加到日期时间并返回一个新的 LocalDateTime 对象。
plusWeeks(long weeksToAdd)LocalDateTime将指定的周数添加到日期时间并返回一个新的 LocalDateTime 对象。
plusDays(long daysToAdd)LocalDateTime将指定的天数添加到日期时间并返回一个新的 LocalDateTime 对象。
plusHours(long hoursToAdd)LocalDateTime将指定的小时数添加到日期时间并返回一个新的 LocalDateTime 对象。
plusMinutes(long minutesToAdd)LocalDateTime将指定的分钟数添加到日期时间并返回一个新的 LocalDateTime 对象。
plusSeconds(long secondsToAdd)LocalDateTime将指定的秒数添加到日期时间并返回一个新的 LocalDateTime 对象。
plusNanos(long nanosToAdd)LocalDateTime将指定的纳秒数添加到日期时间并返回一个新的 LocalDateTime 对象。
minusYears(long yearsToSubtract)LocalDateTime从日期时间中减去指定的年数并返回一个新的 LocalDateTime 对象。
minusMonths(long monthsToSubtract)LocalDateTime从日期时间中减去指定的月数并返回一个新的 LocalDateTime 对象。
minusWeeks(long weeksToSubtract)LocalDateTime从日期时间中减去指定的周数并返回一个新的 LocalDateTime 对象。
minusDays(long daysToSubtract)LocalDateTime从日期时间中减去指定的天数并返回一个新的 LocalDateTime 对象。
minusHours(long hoursToSubtract)LocalDateTime从日期时间中减去指定的小时数并返回一个新的 LocalDateTime 对象。
minusMinutes(long minutesToSubtract)LocalDateTime从日期时间中减去指定的分钟数并返回一个新的 LocalDateTime 对象。
minusSeconds(long secondsToSubtract)LocalDateTime从日期时间中减去指定的秒数并返回一个新的 LocalDateTime 对象。
minusNanos(long nanosToSubtract)LocalDateTime从日期时间中减去指定的纳秒数并返回一个新的 LocalDateTime 对象。
toLocalDate()LocalDate将日期时间转换为本地日期。
toLocalTime()LocalTime将日期时间转换为本地时间。
format(DateTimeFormatter formatter)String将日期时间格式化为指定格式的字符串。
isBefore(LocalDateTime other)boolean检查日期时间是否在指定日期时间之前。
isAfter(LocalDateTime other)boolean检查日期时间是否在指定日期时间之后。
isEqual(LocalDateTime other)boolean检查日期时间是否与指定日期时间相等。
compareTo(LocalDateTime other)int比较日期时间与指定日期时间的顺序。
equals(Object obj)boolean检查日期时间是否与指定对象相等。
hashCode()int返回日期时间的哈希码。
toString()String返回日期时间的字符串表示形式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值