Java教程-Java LocalDateTime类

Java LocalDateTime类
Java LocalDateTime类是一个不可变的日期时间对象,表示日期时间,其默认格式为yyyy-MM-dd-HH-mm-ss.zzz。它继承自Object类并实现了ChronoLocalDateTime接口。
Java LocalDateTime类的声明
让我们看一下java.time.LocalDateTime类的声明。
public final class LocalDateTime extends Object
implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable
Java LocalDateTime的方法
Method | Description |
---|---|
String format(DateTimeFormatter formatter) | 它用于使用指定的格式化程序格式化此日期时间。 |
int get(TemporalField field) | 它用于从此日期时间获取指定字段的值作为 int。 |
LocalDateTime minusDays(long days) | 它用于返回此 LocalDateTime 的副本,并减去指定的天数。 |
static LocalDateTime now() | 它用于从默认时区的系统时钟获取当前日期时间。 |
static LocalDateTime of(LocalDate date, LocalTime time) | 它用于从日期和时间获取 LocalDateTime 的实例。 |
LocalDateTime plusDays(long days) | 它用于返回此 LocalDateTime 的副本,其中添加了指定的天数。 |
boolean equals(Object obj) | 它用于检查此日期时间是否等于另一个日期时间。 |
Java LocalDateTime示例
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
Java LocalDateTime示例:now()
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);
}
}
输出:
Java LocalDateTime示例:get()
输出:
14-01-2017 11:42:32
Java LocalDateTime示例:minusDays()
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class LocalDateTimeExample3 {
public static void main(String[] args) {
LocalDateTime a = LocalDateTime.of(2017, 2, 13, 15, 56);
System.out.println(a.get(ChronoField.DAY_OF_WEEK));
System.out.println(a.get(ChronoField.DAY_OF_YEAR));
System.out.println(a.get(ChronoField.DAY_OF_MONTH));
System.out.println(a.get(ChronoField.HOUR_OF_DAY));
System.out.println(a.get(ChronoField.MINUTE_OF_DAY));
}
}
输出:
1
44
13
15
956
Java LocalDateTime示例:plusDays()
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample4 {
public static void main(String[] args) {
LocalDateTime datetime1 = LocalDateTime.of(2017, 1, 14, 10, 34);
LocalDateTime datetime2 = datetime1.minusDays(100);
System.out.println("Before Formatting: " + datetime2);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
String formatDateTime = datetime2.format(format);
System.out.println("After Formatting: " + formatDateTime );
}
}
Test it Now
Output:
Before Formatting: 2016-10-06T10:34
After Formatting: 06-10-2016 10:34
Java LocalDateTime Example: plusDays()
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample5 {
public static void main(String[] args) {
LocalDateTime datetime1 = LocalDateTime.of(2017, 1, 14, 10, 34);
LocalDateTime datetime2 = datetime1.plusDays(120);
System.out.println("Before Formatting: " + datetime2);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
String formatDateTime = datetime2.format(format);
System.out.println("After Formatting: " + formatDateTime );
}
}
输出:
Before Formatting: 2017-05-14T10:34
After Formatting: 14-05-2017 10:34