Introduction
Year
in Java, part of the java.time
package, represents a year in the ISO-8601 calendar system. It provides various methods for manipulating and retrieving information about years.
Table of Contents
- What is
Year
? - Creating
Year
Instances - Common Methods
- Examples of
Year
- Conclusion
1. What is Year?
Year
is an immutable class that represents a year, useful for handling date and time operations where only the year is relevant.
2. Creating Year Instances
You can create Year
instances in several ways:
Year.now()
: Obtains the current year from the system clock.Year.of(int year)
: Creates an instance ofYear
with the specified year.Year.parse(CharSequence text)
: Parses a string to aYear
using the ISO-8601 format.
3. Common Methods
getValue()
: Returns the numeric value of the year.isLeap()
: Checks if the year is a leap year.plusYears(long yearsToAdd)
: Returns a copy of thisYear
with the specified number of years added.minusYears(long yearsToSubtract)
: Returns a copy of thisYear
with the specified number of years subtracted.length()
: Returns the length of the year in days (365 or 366).atDay(int dayOfYear)
: Combines this year with a day-of-year to create aLocalDate
.
4. Examples of Year
Example 1: Getting the Current Year
This example demonstrates how to get the current year using Year.now()
.
import java.time.Year;
public class CurrentYearExample {
public static void main(String[] args) {
Year currentYear = Year.now();
System.out.println("Current Year: " + currentYear);
}
}
Output:
Current Year: 2024
Example 2: Creating a Specific Year
Here, we create a specific year using Year.of(int year)
.
import java.time.Year;
public class SpecificYearExample {
public static void main(String[] args) {
Year year = Year.of(2023);
System.out.println("Specific Year: " + year);
}
}
Output:
Specific Year: 2023
Example 3: Parsing a Year String
This example shows how to parse a string into a Year
using Year.parse(CharSequence text)
.
import java.time.Year;
public class ParseYearExample {
public static void main(String[] args) {
Year year = Year.parse("2023");
System.out.println("Parsed Year: " + year);
}
}
Output:
Parsed Year: 2023
Example 4: Checking if a Year is a Leap Year
In this example, we demonstrate how to check if a year is a leap year using isLeap()
.
import java.time.Year;
public class LeapYearCheckExample {
public static void main(String[] args) {
Year year = Year.of(2024);
boolean isLeap = year.isLeap();
System.out.println("Is Leap Year: " + isLeap);
}
}
Output:
Is Leap Year: true
Example 5: Adding and Subtracting Years
This example demonstrates how to add and subtract years from a Year
.
import java.time.Year;
public class AddSubtractYearsExample {
public static void main(String[] args) {
Year year = Year.of(2023);
Year nextYear = year.plusYears(1);
Year previousYear = year.minusYears(1);
System.out.println("Current Year: " + year);
System.out.println("Next Year: " + nextYear);
System.out.println("Previous Year: " + previousYear);
}
}
Output:
Current Year: 2023
Next Year: 2024
Previous Year: 2022
Example 6: Getting the Length of a Year
This example shows how to retrieve the length of a year in days.
import java.time.Year;
public class YearLengthExample {
public static void main(String[] args) {
Year year = Year.of(2023);
int length = year.length();
System.out.println("Length of Year: " + length + " days");
}
}
Output:
Length of Year: 365 days
Conclusion
The Year
class in Java is used for handling operations related to years. It provides methods to check for leap years, manipulate year values, and convert to LocalDate
. Using Year
can lead to more precise and readable code when dealing with year-based calculations.
Comments
Post a Comment
Leave Comment