Java LocalDateTime类

本文概述

  • Java LocalDateTime类声明
  • Java LocalDateTime示例
  • Java LocalDateTime示例:now()
  • Java LocalDateTime示例:get()
  • Java LocalDateTime示例:minusDays()
  • Java LocalDateTime示例:plusDays()
Java LocalDateTime类是一个不可变的日期时间对象, 代表日期时间, 默认格式为yyyy-MM-dd-HH-mm-ss.zzz。它继承了对象类并实现了ChronoLocalDateTime接口。
Java LocalDateTime类声明我们来看一下java.time.LocalDateTime类的声明。
public final class LocalDateTime extends Object implements Temporal, TemporalAdjuster, ChronoLocalDateTime< LocalDate> , Serializable

Java LocalDateTime的方法
方法 描述
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); } }

【Java LocalDateTime类】立即测试
输出:
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); } }

立即测试
输出:
14-01-2017 11:42:32

Java LocalDateTime示例:get()
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示例:minusDays()
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 ); } }

立即测试
输出:
Before Formatting: 2016-10-06T10:34 After Formatting: 06-10-2016 10:34

Java LocalDateTime示例: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

    推荐阅读