Java教程

Java LocalDateTime

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) 它用于从该日期时间以整数形式获取指定字段的值。
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);
    }
}
输出:
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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4