📜  Java的.time.Year类在Java中

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

Java的.time.Year类在Java中

Java.time.Year 类表示 ISO-8601 日历系统中的一年,例如 2021。Year 是表示一年的不可变日期时间对象。此类不存储或代表月,日时间或时区。此类代表的年份遵循如下的预测编号系统:

语法:类声明

public final class Year extends Object implements Temporal, TemporalAdjuster, Comparable, Serializable

现在让我们讨论这个 Year 类的所有方法,如下以表格格式显示。稍后,我们将分别讨论它们。

MethodDescription
adjustsIntro()Adjusts the specified temporal object to have this year.
atDay()Creates a local date with day number equal to the number in the Year class object
atMonth()Combine this year with a month to create a YearMonth 
atMonthDay()Combine this year with a month date to create a LocalDate 
compareTo()Compares the two-year objects.
equals()Checks if two-year objects are equal.
format()Formats the year using specified datetime formatter.
get()Gets the value of the specified field from this year.
getLong()Getting the value of a specified field from this year as a long 
getValue()Returns the integer value of the year.
hashCode()Returns the hashcode of the year object.
isAfter()Checks whether the year is after year1.
isBefore()Checks whether the year is before year1.
isLeap()Checks whether the year is a leap year or not.
isSupported()Checks if the specified temporal field is supported.
isValidMonthDay()Checks if the month-day is valid for this year
length()Returns the number of days in the year.
minus()Returns the copy of this year with the specified amount subtracted
minusYears()Subtracts a number of years from a year and returns a new Year object.
now()Gets current year and creates a Year object.
of()

“Creates Year object with year equal to the number.



0 is used for 0 AD whereas -1 is used for 1 BC.”

parse()Obtains an instance of Year from a text string such as 2021.
plus()Returns a copy of this year with the specified number of years added.
plusYears()Adds num years to a year and returns a new Year object.
query()Queries this year using the specified query.
range()getting the range of valid values for the specified field. 
toString()Returns the current year as a string.
until()Calculates the amount of time until another year in terms of the specified unit.
with()Returns an adjusted copy of the year.

让我们详细讨论一下这个类的一些常用方法,以便更好地理解,以便实现其余的方法。在这里我们将要讨论区R的主要方法即是弗洛w ^ S:

  1. 在天()
  2. 哈希码()
  3.  之后()
  4. 是之前()
  5. isLeap()
  6. 现在()
  7. 减()

方法一: Java中Year 类的atDay() 方法将当前年份与作为参数传递给它的一年中的某一天结合起来创建一个LocalDate。

语法

public LocalDate atDay(int dayOfYear)

参数:此方法接受一年中的单个参数日。这是一年中使用的日子。它可以采用从 1 到 365-366 的值。

返回值: 它返回由当前年份和作为参数传递给函数的年份日期组成的本地日期。

异常:此方法抛出 DateTimeException,如果参数中传递的年份无效,即零或更小、366 或更大或等于 366 且当前年份不是闰年。

方法二: hashCode()



它以整数形式返回哈希码值。喜欢的HashMap,HashSet的,哈希表等基础的集合-哈希码值在哈希大多采用。必须在每个覆盖 equals() 方法的类中覆盖此方法。
句法:

public int hashCode()

// This method returns the hash code value 
// for the object on which this method is invoked.

方法三:

Java的 Year 类的 isAfter() 方法用于检查当前的 Year 对象是否在指定为该方法的参数的 Year 之后。

语法

public boolean isAfter(Year otherYear)

Parameter :它接受另一个年份的单个参数,与当前 Year 对象进行比较。

返回值:如果此 Year 对象的值在指定为方法参数的 Year 对象的值之后,则返回布尔 True 值,否则返回 False。

方法四:

Java中 Year 类的 isBefore() 方法用于检查当前 Year 对象是否在指定为该方法参数的 Year 之前。

语法

public boolean isBefore(Year otherYear)

参数:它接受一个参数 otherYear,当前的 Year 对象将与它进行比较。



返回值:如果此 Year 对象的值在指定为方法参数的 Year 对象的值之前,则返回布尔 True 值,否则返回 False。

方法五:

Java中 Year 类的 isLeap() 方法用于根据预测日历系统规则检查此 Year 对象是否为闰年。

语法

public boolean isLeap()

参数:此方法不接受任何参数。

返回值:如果根据预测日历系统规则,此 Year 对象的值为闰年,则返回布尔值 True,否则返回 False。

方法六:

Java中 Year 类的 now() 方法用于从默认时区的系统时钟返回当前年份。

语法



public static Year now()

or,
public static Year now(Clock clock)

or,
public static Year now(ZoneId zone)

Parameter :该方法的参数是可选的,如上述语法所示。

返回值:如果未指定参数,则从默认时区的系统时钟返回当前年份,否则使用指定的时钟和时区返回当前年份。它永远不会返回 NULL 值

方法七:

Year 类的 minus() 方法用于从该 Year 对象中减去指定数量的单位后返回该年份的副本。如果不能从 Year 中减去指定的单位,则抛出异常。此实例是不可变的,不受此方法调用的影响。

句法:

public Year minus(long amountToSubtract, TemporalUnit unit)

参数:此方法接受以下两个参数:

  • 减去的金额
    • 此参数表示要从结果中减去的单位量。
  • 单元
    • 此参数表示要减去的金额的单位。

返回值:此方法返回基于这一年减去指定金额的年份。

异常:此方法抛出以下异常:

  • 日期时间异常
    • 如果不能进行减法,则抛出此异常。
  • 不支持的TemporalTypeException
    • 如果该单元不受支持,则会引发此异常。
  • 算术异常
    • 如果发生数字溢出,则会引发此异常。

您可以为 plus() 方法关联相同的内容。至此,我们已经结束了对方法的讨论。现在是时候实现相同的内容来演示 Year 类了。

执行:

例子

Java
// Java Program to illustrate java.time.Year class
import java.time.*;
 
// Importing all classes from
// time package to get other functionalities
import java.time.Year;
 
//  Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Object 1
        // Creating an Year class object
 
        // getting the current year each
        // using the now() method
        Year y = Year.now();
 
        // Print and display the current year
        System.out.println("Current year: " + y);
 
        // Object 2
        // Creating another Year class object Object
        // Here year 2020 as passed as an argument
        Year y1 = Year.of(2020);
 
        // Object 3
        // Creating another object of year class
        // again passing year 2022 as an argument
        Year y2 = Year.of(2022);
 
        // Now, creating a LocalDate object and
        // getting the local date at the day
        // passed as an argument
        // Custom input argument = 350
        LocalDate l = y.atDay(350);
 
        // Print the local date
        System.out.println("Local date: " + l);
 
        // Now, creating a YearMonth
        // and retrieving month using atMonth() method
        // by passing argument to the method
        // Custom input argument- 6
        YearMonth ym = y.atMonth(6);
 
        // Print the year and the month
        System.out.println("Year and month: " + ym);
 
        // Printing number of days of current Year
        System.out.println("Number of days: " + y.length());
 
        // Checking if Year y
        // is before Year y2
        System.out.println("2021 is before 2022: "
                           + y.isBefore(y2));
        // check if Year y
        // is after Year y1
        System.out.println("2021 is after 2020: "
                           + y.isAfter(y1));
 
        // Now, printing hashcode of the year
        // using the hashcode() method
        System.out.println("Hashcode of the year: "
                           + y.hashCode());
 
        // Checking whether the year is leap year or not
        System.out.println("2020 is leap year: "
                           + y1.isLeap());
 
        // Adding 150 years to the current year
        System.out.println("2021 - 155 years: "
                           + y.minusYears(155));
        // Subtracting 150 years from the current year
        System.out.println("2021 + 145 years: "
                           + y.plusYears(145));
    }
}


输出
Current year: 2021
Local date: 2021-12-16
Year and month: 2021-06
Number of days: 365
2021 is before 2022: true
2021 is after 2020: true
Hashcode of the year: 2021
2020 is leap year: true
2021 - 155 years: 1866
2021 + 145 years: 2166