📜  Java的.time.LocalDate类在Java中

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

Java的.time.LocalDate类在Java中

Java是最流行的编程语言和广泛使用的编程语言。 Java用于各种应用程序,如移动应用程序、桌面应用程序、Web 应用程序。在这个Java Java.time.LocalDate 类被导入,它代表显示当前日期。

Java .time :它是用于处理当前日期和时间 API 的包。

班级:

ClassDescription
LocalDate  It is a class that represents a date. 
DateTimeFormatter  It is a class used to format and parse date and time.

方法:

Methods



Description

adjustInto(Temporal temporal)This method adjusts the specified temporal object to having the same date as this object.
 atStartOfDay()This method combines this date with the time of midnight to create a LocalDateTime at the start of this date.
atStartOfDay(ZoneId zone)This method returns a zoned date-time from this date at the earliest valid time according to the rules in the time-zone.
 atTime(int hour, int minute)This method combines this date with a time to create a LocalDateTime.
 atTime(int hour, int minute, int second)This method combines this date with a time to create a LocalDateTime.
 compareTo(ChronoLocalDate other)This method compares this date to another date.
equals(Object obj)This method checks if this date is equal to another date.
format(DateTimeFormatter formatter)This method formats this date using the specified formatter.
from(TemporalAccessor temporal)This method obtains an instance of LocalDate from a temporal object.
 get(TemporalField field)This method gets the value of the specified field from this date as an int.
 getChronology()This method gets the chronology of this date, which is the ISO calendar system.
getDayOfMonth()This method gets the day-of-month field.
 getDayOfWeek()This method gets the day-of-week field, which is an enum DayOfWeek.
getDayOfYear()This method gets the day-of-year field.
 getEra()This method gets the era applicable at this date.
getLong(TemporalField field)This method gets the value of the specified field from this date as a long.
getMonth()This method gets the month-of-year field using the Month enum.
getMonthValue()This method gets the month-of-year field from 1 to 12.
 getYear()This method gets the year field.
hashCode()A hash code for this date.
isAfter(ChronoLocalDate other)This method checks if this date is after the specified date.
isBefore(ChronoLocalDate other)This method checks if this date is before the specified date.
isEqual(ChronoLocalDate other)This method checks if this date is equal to the specified date.
isLeapYear()This method checks if the year is a leap year, according to the ISO proleptic calendar system rules.
isSupported(TemporalField field)This method checks if the specified field is supported.
lengthOfMonth()This method returns the length of the month represented by this date.
lengthOfYear()This method returns the length of the year represented by this date.
now() It is a method used to return the instance of LocalDateTime class.
ofPattern()It is a method used with DateTimeFormatter to format and parse date and time. It accepts all types or sorts of value, to display in a different format.
query(TemporalQuery query)This method queries this date using the specified query.
range(TemporalField field)This method gets the range of valid values for the specified field.
 toEpochDay()This method converts this date to Epoch Day.
withDayOfMonth(int dayOfMonth)This method returns a copy of this LocalDate with the day-of-month altered.
withDayOfYear(int dayOfYear)This method returns a copy of this LocalDate with the day-of-year altered.
 withMonth(int month)This method returns a copy of this LocalDate with the month-of-year altered.
 withYear(int year)This method returns a copy of this LocalDate with the year altered.

方法

  1. 使用Java .time 包导入Java.time.LocalDateTime 和Java .time.format.DateTimeFormatter 等类。
  2. 使用 LocalDateTime.now() 和 now() 方法。
  3. 使用 DateTimeFormatter.ofPattern 对当前日期进行排序。
  4. 显示存储在变量中的日期。

下面是问题陈述的实现:

Java
// import package used to work with current date and time
// api.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // now() is a method to return the
        // instance of LocalDateTime class.
        LocalDateTime localDate = LocalDateTime.now();
        // DateTimeFormatter class used to format and
        // parse date and time. ofPattern() is a method
        // used with DateTimeFormatter to format and
        // parse date and time.
        DateTimeFormatter dateformatter
            = DateTimeFormatter.ofPattern("MM dd, YYYY");
        // display the date
        System.out.println(dateformatter.format(localDate));
    }
}


Java
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // Parses the date
        LocalDate dt1 = LocalDate.parse("2021-01-07");
        LocalDate result = dt1.withDayOfYear(01);
 
        // Prints the date with year
        System.out.println("The date with day of year is: "
                           + result);
    }
}


输出
03 16, 2021


Java

Java

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // Parses the date
        LocalDate dt1 = LocalDate.parse("2021-01-07");
        LocalDate result = dt1.withDayOfYear(01);
 
        // Prints the date with year
        System.out.println("The date with day of year is: "
                           + result);
    }
}
输出
The date with day of year is: 2021-01-01