📜  Java的.time.LocalDateTime类在Java中

📅  最后修改于: 2022-05-13 01:55:41.321000             🧑  作者: Mango

Java的.time.LocalDateTime类在Java中

Java 8 中引入的Java .time.LocalDateTime 类表示没有时区信息的本地日期时间对象。 Java的 LocalDateTime 类是一个不可变的日期时间对象,它表示yyyy-MM-dd-HH-mm-ss.zzz格式的日期。它实现了 ChronoLocalDateTime 接口并继承了对象类。

无论何时需要在没有时区引用的情况下表示时间,我们都可以使用 LocalDateTime 实例。例如,LocalDateTime 可用于在任何应用程序中启动批处理作业。作业将在服务器所在时区的固定时间运行。注意 LocalDateTime 实例是不可变的和线程的。

语法:类声明

该类的方法如下:

MethodDescription
format() It is used to format this date-time using the specified formatter.
get() It is used to get the value of the specified field from this date-time as an int.
minusMinutes()Returns a copy of this LocalDateTime with the specified number of minutes subtracted.
minusYears()Returns a copy of this LocalDateTime with the specified number of years subtracted.
minusDays()Returns a copy of this LocalDateTime with the specified number of days subtracted.
now()It is used to obtain the current date-time from the system clock in the default time zone.
plusHours()Returns a copy of this LocalDateTime with the specified number of hours added.
plusYears()Returns a copy of this LocalDateTime with the specified number of years added.
plusDays()Returns a copy of this LocalDateTime with the specified number of days added.

还有一些修改本地时间的方法如下  in LocalDateTime 可用于获取相对于现有 localdatetime 实例的新 localdatetime 实例。它们分别如下:

plusYears()、plusMonths()、plusDays()、plusHours()、plusMinutes()、plusSeconds()、plusNanos()、minusYears()、minusMonths()、minusDays()、minusHours()、minusMinutes()、minusSeconds() ), 减去Nanos()

示例 1:

Java
// Java Program to illustrate LocalDateTime Class of java.time package 
 
// Importing LocalDateTime class from java.time package
import java.time.LocalDateTime;
 
// Main class for LocalDateTime
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of LocalDateTime class
        // in the main() method
        LocalDateTime now = LocalDateTime.now();
 
        // Print statement
        System.out.println(now);
 
        // Adding 1 year, 1 month, 1 week and 1 day
        LocalDateTime localDateTime1 = now.plusYears(1)
                                           .plusMonths(1)
                                           .plusWeeks(1)
                                           .plusDays(1);
        // Print statement
        System.out.println(localDateTime1);
 
        // Subtracting 1 year, 1 month, 1 week and 1 day
        LocalDateTime localDateTime2
            = localDateTime1.minusYears(1)
                  .minusMonths(1)
                  .minusWeeks(1)
                  .minusDays(1);
        // Print statement
        System.out.println(localDateTime2);
 
        // Adding 1 hour, 1 minute, 1 second and 100
        // nanoseconds
        LocalDateTime localDateTime3
            = localDateTime2.plusHours(1)
                  .plusMinutes(1)
                  .plusSeconds(1)
                  .plusNanos(100);
        // Print statement
        System.out.println(localDateTime3);
 
        // Subtracting 1 hour, 1 minute, 1 second and 100
        // nanoseconds
        LocalDateTime localDateTime4
            = localDateTime3.minusHours(1)
                  .minusMinutes(1)
                  .minusSeconds(1)
                  .minusNanos(100);
        // Print statement
        System.out.println(localDateTime4);
    }
}


Java
// Java Program to illustrate LocalDateTime Class
// of java.time package by creating specific time
 
// Importing required classes from resp packages
import java.time.*;
import java.time.format.*;
 
// main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Milliseconds
        LocalDateTime localDateTime1 = LocalDateTime.of(
            2021, 04, 24, 14, 33, 48, 123456789);
        // Print statement
        System.out.println(localDateTime1);
 
        // Month
        LocalDateTime localDateTime2 = LocalDateTime.of(
            2021, Month.APRIL, 24, 14, 33, 48, 123456789);
        // Print statement
        System.out.println(localDateTime2);
 
        // Seconds
        LocalDateTime localDateTime3 = LocalDateTime.of(
            2021, Month.APRIL, 24, 14, 33, 48);
        // Print statement
        System.out.println(localDateTime3);
 
        // Minutes
        LocalDateTime localDateTime4 = LocalDateTime.of(
            2021, Month.APRIL, 24, 14, 33);
        // Print statement
        System.out.println(localDateTime4);
 
        // Local date + Local time
        LocalDate date = LocalDate.of(2021, 04, 24);
        LocalTime time = LocalTime.of(10, 34);
 
        LocalDateTime localDateTime5
            = LocalDateTime.of(date, time);
        // Print statement
        System.out.println(localDateTime5);
    }
}


Java
// Java Program to illustrate LocalDateTime Class by
// Formatting LocalDateTime to string
 
// Importing all classes from java.time package
import java.time.LocalDateTime;
import java.time.format.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of DateTimeFormatter class
        DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern(
                "yyyy-MM-dd HH:mm:ss a");
 
        // Creating an object of LocalDateTime class
        // and getting local date and time using now()
        // method
        LocalDateTime now = LocalDateTime.now();
 
        // Formatting LocalDateTime to string
        String dateTimeString = now.format(formatter);
 
        // Print and Display
        System.out.println(dateTimeString);
    }
}


输出:

示例 2:创建指定时间



Java

// Java Program to illustrate LocalDateTime Class
// of java.time package by creating specific time
 
// Importing required classes from resp packages
import java.time.*;
import java.time.format.*;
 
// main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Milliseconds
        LocalDateTime localDateTime1 = LocalDateTime.of(
            2021, 04, 24, 14, 33, 48, 123456789);
        // Print statement
        System.out.println(localDateTime1);
 
        // Month
        LocalDateTime localDateTime2 = LocalDateTime.of(
            2021, Month.APRIL, 24, 14, 33, 48, 123456789);
        // Print statement
        System.out.println(localDateTime2);
 
        // Seconds
        LocalDateTime localDateTime3 = LocalDateTime.of(
            2021, Month.APRIL, 24, 14, 33, 48);
        // Print statement
        System.out.println(localDateTime3);
 
        // Minutes
        LocalDateTime localDateTime4 = LocalDateTime.of(
            2021, Month.APRIL, 24, 14, 33);
        // Print statement
        System.out.println(localDateTime4);
 
        // Local date + Local time
        LocalDate date = LocalDate.of(2021, 04, 24);
        LocalTime time = LocalTime.of(10, 34);
 
        LocalDateTime localDateTime5
            = LocalDateTime.of(date, time);
        // Print statement
        System.out.println(localDateTime5);
    }
}

输出:

示例 3 :将 LocalDateTime 格式化为字符串

要将本地时间格式化为所需的字符串表示形式,请使用 LocalDateTime.format(DateTimeFormatter) 方法。

Java

// Java Program to illustrate LocalDateTime Class by
// Formatting LocalDateTime to string
 
// Importing all classes from java.time package
import java.time.LocalDateTime;
import java.time.format.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of DateTimeFormatter class
        DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern(
                "yyyy-MM-dd HH:mm:ss a");
 
        // Creating an object of LocalDateTime class
        // and getting local date and time using now()
        // method
        LocalDateTime now = LocalDateTime.now();
 
        // Formatting LocalDateTime to string
        String dateTimeString = now.format(formatter);
 
        // Print and Display
        System.out.println(dateTimeString);
    }
}

输出: