📜  Java中LocalDate、LocalTime和LocalDateTime类中的常用方法

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

Java中LocalDate、LocalTime和LocalDateTime类中的常用方法

Java提供了三个与日期相关的最重要的类,即LocalDateLocalTime 、 LocalDateTime ,这使得Java的日期处理变得非常容易,因为为了使用这些类,我们需要导入Java.time” 包是日期、时间、瞬间和持续时间的主要 API。

插图:

1. java.time.* 
   // To include all classes
   
2. java.time.LocalDate
   // for LocalDate
   
3. java.time.LocalDateTime 
   // for LocalDateTime
   
4. java.time.time 
   // for LocalTime
ClassDescription 
LocalDateLocalDate class can hold the Date only. For example, say be it 2021-02-28
LocalTimeLocalTime class can hold the Time only. For example, say be it 19:32:25.457826
LocalDateTime

LocalDateTime class holds both the Date and Time. For example, say be it 2021-02-28T19:32:25.457826. 

In this format, Before T is the date, and after T is the Time.

人们普遍认为导入包中所有不必要的文件被认为是不好的做法。因此,我们应该只导入需要的类。这些类中存在各种方法来根据类处理日期和时间。让我们讨论 LocalDate 类中最常见的方法,称为now()方法。



方法: now() 方法 

LocalDate 类的 now() 方法用于从默认时区的系统时钟获取当前日期。此方法将根据具有默认时区的系统时钟返回 LocalDate 以获取当前日期。

句法:

public static LocalDate now()

返回值:此方法使用系统时钟和默认时区返回当前日期。

示例 1:

Java
// Java Program to illustrate Commonly used methods in classes
// LocalDate, LocalTime and LocalDateTime Classes
 
// Importing input output classes
import java.io.*;
// Importing LocalDate, LocalTime, LocalDateTime classes
// from java.time package 
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main (String[] args) {
         
        // Creating instance of LocalDate class
        // using now() method
        LocalDate presentDate = LocalDate.now();
         
        // Print and display present date
        System.out.println(presentDate);
         
        // Creating instance of LocalDateTime class
        // using now() method
        LocalDateTime present = LocalDateTime.now();
         
        System.out.println(present);
         
        // Creating instance of LocalTime class
        // again using now() method
        LocalTime presentTime = LocalTime.now();
         
        // Print and display the current time 
        System.out.println(presentTime);
    }
}


Java
// Java Program to illustrate Commonly used methods in
// classes LocalDate, LocalTime and LocalDateTime Classes
 
//  Importing input output classes
import java.io.*;
// Importing java.time package to import classes
// LocalDate, LocalTime and LocalDateTime
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of LocalDate
        LocalDate presentDate = LocalDate.now();
 
        System.out.println(presentDate);
 
        // Creating object of LocalDateTime
        LocalDateTime present = LocalDateTime.now();
 
        System.out.println(present);
 
        // Creating object of LocalTime
        LocalTime presentTime = LocalTime.now();
 
        System.out.println(presentTime);
 
        // Implementing the LocalDate class methods
        // All methods of the table are shown below
 
        // Print the the day of the month
        System.out.println(presentDate.getDayOfMonth());
 
        // Print the weekday
        System.out.println(presentDate.getDayOfWeek());
 
        // Print the day w.r.t. the year
        System.out.println(presentDate.getDayOfYear());
 
        // Print the name of the month
        System.out.println(presentDate.getMonth());
 
        // Print the name of the month
        System.out.println(presentDate.getMonthValue());
 
        // Print the boolean value (true/false)
        System.out.println(presentDate.isLeapYear());
 
        // Print the number of days in that year
        System.out.println(presentDate.lengthOfYear());
 
        // Print the
        System.out.println(presentDate.lengthOfMonth());
 
        // Print the new date after adding the number of
        // days to the current date.
        System.out.println(presentDate.plusDays(50));
 
        // Print the new date after adding the number of
        // months to the current date.
        System.out.println(presentDate.plusMonths(50));
 
        // Print the new date after adding the number of
        // years to the current date.
        System.out.println(presentDate.plusYears(50));
 
        // Similarly for the rest of them.
        System.out.println(presentDate.minusDays(50));
        System.out.println(presentDate.minusMonths(50));
        System.out.println(presentDate.minusYears(50));
 
        // Implementing methods which are available in
        // LocalTine and LocalDateTime
 
        System.out.println(present.plusHours(100));
        System.out.println(present.plusMinutes(1000));
        System.out.println(present.plusSeconds(100000));
        System.out.println(present.plusNanos(1000000));
    }
}


输出
2021-02-28
2021-02-28T14:16:07.181034
14:16:07.181230

我们将在进一步讨论的实现部分的单个代码中讨论下表中的其余实用方法。已经讨论了所有类,即 LocalDate、LocalDateTime、LocalTime 类是不可变的,所有修改方法都返回新对象,因此不会更改当前对象的值。

LocalDate 类提供的一些实用方法如下:

MethodsDescription
getDayOfMonth()Returns the day of the month. For example, say it be 28
getDayOfWeek()Returns the weekday. For example, say it be SUNDAY
getDayOfYear()Returns the day w.r.t. the year. For example, say it be 59
getMonth()Returns the name of the month. For example, say it be FEBRUARY
getMonthValue()Returns the numeric value of the month. For example, say it be 2
isLeapYear()Returns boolean value (true/false). For example, say it be false.
lengthOfYear()Returns the number of days in that year. For example, say it be 365.
lengthOfMonth()Returns the number of days in that year. For example, say ut be 28.
plusDays(numberOfDaysToBeAdded)Returns a new date after adding the number of days to the current date.
plusMonths(numberOfMonthsToBeAdded)Returns a new date after adding the number of months to the current date.
plusYears(numberOfYearsToBeAdded)Returns a new date after adding the number of years to the current date.

可以使用类似的方法减去日期。减日(),减月(),减年()。这些函数也可用于 LocalDateTime 类,增加了一些与时间相关的功能。例如 plusHours()、plusMinutes()、plusSeconds()、plusNanos() 等等。

示例 2:

Java

// Java Program to illustrate Commonly used methods in
// classes LocalDate, LocalTime and LocalDateTime Classes
 
//  Importing input output classes
import java.io.*;
// Importing java.time package to import classes
// LocalDate, LocalTime and LocalDateTime
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of LocalDate
        LocalDate presentDate = LocalDate.now();
 
        System.out.println(presentDate);
 
        // Creating object of LocalDateTime
        LocalDateTime present = LocalDateTime.now();
 
        System.out.println(present);
 
        // Creating object of LocalTime
        LocalTime presentTime = LocalTime.now();
 
        System.out.println(presentTime);
 
        // Implementing the LocalDate class methods
        // All methods of the table are shown below
 
        // Print the the day of the month
        System.out.println(presentDate.getDayOfMonth());
 
        // Print the weekday
        System.out.println(presentDate.getDayOfWeek());
 
        // Print the day w.r.t. the year
        System.out.println(presentDate.getDayOfYear());
 
        // Print the name of the month
        System.out.println(presentDate.getMonth());
 
        // Print the name of the month
        System.out.println(presentDate.getMonthValue());
 
        // Print the boolean value (true/false)
        System.out.println(presentDate.isLeapYear());
 
        // Print the number of days in that year
        System.out.println(presentDate.lengthOfYear());
 
        // Print the
        System.out.println(presentDate.lengthOfMonth());
 
        // Print the new date after adding the number of
        // days to the current date.
        System.out.println(presentDate.plusDays(50));
 
        // Print the new date after adding the number of
        // months to the current date.
        System.out.println(presentDate.plusMonths(50));
 
        // Print the new date after adding the number of
        // years to the current date.
        System.out.println(presentDate.plusYears(50));
 
        // Similarly for the rest of them.
        System.out.println(presentDate.minusDays(50));
        System.out.println(presentDate.minusMonths(50));
        System.out.println(presentDate.minusYears(50));
 
        // Implementing methods which are available in
        // LocalTine and LocalDateTime
 
        System.out.println(present.plusHours(100));
        System.out.println(present.plusMinutes(1000));
        System.out.println(present.plusSeconds(100000));
        System.out.println(present.plusNanos(1000000));
    }
}
输出
2021-02-28
2021-02-28T14:54:21.331923
14:54:21.332156
28
SUNDAY
59
FEBRUARY
2
false
365
28
2021-04-19
2025-04-28
2071-02-28
2021-01-09
2016-12-28
1971-02-28
2021-03-04T18:54:21.331923
2021-03-01T07:34:21.331923
2021-03-01T18:41:01.331923
2021-02-28T14:54:21.332923