java.time.LocalDateTime

1 java.time.LocalDateTime介绍

Java LocalDateTime类是一个不可变的日期时间对象,代表日期时间,其默认格式为yyyy-MM-dd-HH-mm-ss.zzz。它继承了对象类并实现了ChronoLocalDateTime接口。

2 java.time.LocalDateTime声明

让我们看看java.time.LocalDateTime类的声明。

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

3 java.time.LocalDateTime方法

方法 描述
Temporal adjustInto(Temporal temporal) 调整指定的时态对象以使其具有与此对象相同的日期和时间。
OffsetDateTime atOffset(ZoneOffset offset) 将此日期时间与偏移时间组合以创建OffsetDateTime。
ZonedDateTime atZone(ZoneId zone) 将此日期时间与时区组合以创建ZonedDateTime。
int compareTo(ChronoLocalDateTime<?> other) 将此日期时间与另一个日期时间进行比较。
boolean equals(Object obj) 检查此日期时间是否等于另一个日期时间。
String format(DateTimeFormatter formatter) 使用指定的格式化程序格式化此日期时间。
static LocalDateTime from(TemporalAccessor temporal) 从时态对象获取LocalDateTime的实例。
int get(TemporalField field) 从此日期时间获取指定字段的值作为int类型值。
int getDayOfMonth() 获取日期字段。
DayOfWeek getDayOfWeek() 获取星期几字段,即枚举DayOfWeek。
int getDayOfYear() 获取日期字段。
int getHour() 获取当日时间字段。
long getLong(TemporalField field) 从此日期时间获取指定字段的值为long值。
Month getMinute() 获取分钟字段。
Month getMonth() 使用Month枚举获取月份字段。
int getMonthValue() 获取1到12之间的月份字段。
int getNano() 获取纳秒级字段。
int getSecond() 获取秒钟字段。
int getYear() 获取年份字段。
int hashCode() 此日期时间的哈希码。
boolean isAfter(ChronoLocalDateTime<?> other) 检查此日期时间是否在指定的日期时间之后。
boolean isBefore(ChronoLocalDateTime<?> other) 检查此日期时间是否在指定的日期时间之前。
boolean isEqual(ChronoLocalDateTime<?> other) 检查此日期时间是否等于指定的日期时间。
boolean isSupported(TemporalField field) 检查是否支持指定的字段。
boolean isSupported(TemporalUnit unit) 检查指定的单元是否受支持。
LocalDateTime minus(long amountToSubtract, TemporalUnit unit) 返回此日期时间的副本,并减去指定的数量。
LocalDateTime minus(TemporalAmount amountToSubtract) 返回此日期时间的副本,并减去指定的数量。
LocalDateTime minusDays(long daysToSubtract) 返回此LocalDateTime的副本,并减去指定的天数。
LocalDateTime minusHours(long hoursToSubtract) 返回此LocalDateTime的副本,并减去指定的小时数。
LocalDateTime minusMinutes(long minutesToSubtract) 返回此LocalDateTime的副本,并减去指定的分钟数。
LocalDateTime minusMonths(long monthsToSubtract) 返回此LocalDateTime的副本,并减去指定的月数。
LocalDateTime minusNanos(long nanos) 返回此LocalDateTime的副本,减去指定的纳秒数。
LocalDateTime minusSeconds(long seconds) 返回此LocalDateTime的副本,并减去指定的秒数。
LocalDateTime minusWeeks(long weeksToSubtract) 返回此LocalDateTime的副本,并减去指定的周数。
LocalDateTime minusYears(long yearsToSubtract) 返回此LocalDateTime的副本,并减去指定的年数。
static LocalDateTime now() 从默认时区中的系统时钟获取当前日期时间。
static LocalDateTime now(Clock clock) 从指定的时钟获得当前日期时间。
static LocalDateTime now(ZoneId zone) 从指定时区的系统时钟获取当前日期时间。
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) 从年,月,日,小时和分钟获得LocalDateTime的实例,将秒钟和纳秒设置为零。
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) 从年,月,日,小时,分钟和秒获得LocalDateTime的实例,将纳秒设置为零。
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) 从年,月,日,小时,分钟,秒和纳秒获得LocalDateTime的实例。
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) 从年,月,日,小时,分钟和秒获得LocalDateTime的实例,将纳秒设置为零。
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) 从年,月,日,小时,分钟,秒和纳秒获得LocalDateTime的实例。
static LocalDateTime of(LocalDate date, LocalTime time) 从日期和时间获取LocalDateTime的实例。
static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) 从1970-01-01T00:00:00Z的纪元获得LocalDateTime的实例。
static LocalDateTime ofInstant(Instant instant, ZoneId zone) 从Instant和区域ID获取LocalDateTime的实例。
static LocalDateTime parse(CharSequence text) 从文本字符串中获取LocalDateTime的实例,例如2007-12-03 T10:15:30。
static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) 使用特定格式化程序从文本字符串中获取LocalDateTime的实例。
LocalDateTime plus(long amountToAdd, TemporalUnit unit) 返回此日期时间的副本,并添加指定的数量。
LocalDateTime plus(TemporalAmount amountToAdd) 返回此日期时间的副本,并添加指定的数量。
LocalDateTime plusDays(long daysToAdd) 返回此LocalDateTime的副本,并添加指定的天数。
LocalDateTime plusHours(long hoursToAdd) 返回此LocalDateTime的副本,并添加指定的小时数。
LocalDateTime plusMinutes(long minutesToAdd) 返回此LocalDateTime的副本,并添加指定的分钟数。
LocalDateTime plusMonths(long monthsToAdd) 返回此LocalDateTime的副本,并添加指定的月份数。
LocalDateTime plusNanos(long nanos) 返回此LocalDateTime的副本,其中添加了指定的纳秒数。
LocalDateTime plusSeconds(long seconds) 返回此LocalDateTime的副本,并添加指定的秒数。
LocalDateTime plusWeeks(long weeksToAdd) 返回此LocalDateTime的副本,并添加指定的周数。
LocalDateTime plusYears(long yearsToAdd) 返回此LocalDateTime的副本,其中添加了指定的年数。
<R> R query(TemporalQuery<R> query) 使用指定的查询查询此日期时间。
ValueRange range(TemporalField field) 获取指定字段的有效值范围。
LocalDate toLocalDate() 获取此日期时间的LocalDate部分。
LocalTime toLocalTime() 获取此日期时间的LocalTime部分。
String toString() 将此日期输出为字符串,例如2007-12-03T10:15:30。
LocalDateTime truncatedTo(TemporalUnit unit) 返回此LocalDateTime的副本,并截断时间。
long until(Temporal endExclusive, TemporalUnit unit) 根据指定的单位计算到另一个日期时间的时间量。
LocalDateTime with(TemporalAdjuster adjuster) 返回此日期时间的调整副本。
LocalDateTime with(TemporalField field, long newValue) 返回此日期时间的副本,并将指定字段设置为新值。
LocalDateTime withDayOfMonth(int dayOfMonth) 返回此LocalDateTime的副本,其中包含每日更改的日期。
LocalDateTime withDayOfYear(int dayOfYear) 返回此LocalDateTime的副本,其中包含日期更改。
LocalDateTime withHour(int hour) 返回此LocalDateTime的副本,并更改日期。
LocalDateTime withMinute(int minute) 返回此LocalDateTime的副本,并更改了分钟。
LocalDateTime withMonth(int month) 返回此LocalDateTime的副本,其中包含已更改的年份。
LocalDateTime withNano(int nanoOfSecond) 返回此LocalDateTime的副本,并更改了纳秒。
LocalDateTime withSecond(int second) 返回此LocalDateTime的副本,并更改秒钟。
LocalDateTime withYear(int year) 返回此LocalDateTime的副本,其中年份已更改。

4 java.time.LocalDateTime案例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.time.LocalDateTime;  
import java.time.format.DateTimeFormatter;  
public class LocalDateTimeExample1 {  
    public static void main(String[] args) {  
        LocalDateTime now = LocalDateTime.now();  
        System.out.println("Before Formatting: " + now);  
        DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
        String formatDateTime = now.format(format);  
        System.out.println("After Formatting: " + formatDateTime);  
    }  
}  

输出结果为:

Before Formatting: 2017-01-13T17:09:42.411
After Formatting: 13-01-2017 17:09:42

5 java.time.LocalDateTime案例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.time.LocalDateTime;  
import java.time.format.DateTimeFormatter;  
public class LocalDateTimeExample2 {  
  public static void main(String[] args) {  
    LocalDateTime datetime1 = LocalDateTime.now();  
    DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
    String formatDateTime = datetime1.format(format);   
    System.out.println(formatDateTime);    
  }  
}  

输出结果为:

14-01-2017 11:42:32

 

热门文章

优秀文章