📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The Year class is an immutable date-time object that represents a year. Any field that can be derived from a year can be obtained. This class is immutable and thread-safe.
Java Year Class Diagram
Java Year Class Methods/APIs with Examples
package net.javaguides.date;
import java.time.Clock;
import java.time.LocalDate;
import java.time.Year;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalQueries;
/**
* Class demonstrates the usage of Year methods with examples
*
* @author Ramesh Fadatare
*
*/
public class YearMethodsExample {
public static void main(String[] args) {
YearMethodsExample example = new YearMethodsExample();
example.withMethodExample();
example.untilMethodExample();
example.toStringMethodExample();
example.rangeMethodExample();
example.queryMethodExample();
example.plusYearsMethodExample();
example.ofMethodExample();
example.nowMethod();
example.now();
example.nowMethodExample();
example.minusYearsMethodExample();
example.minusMethodExample();
example.isLeapMethodExample();
example.isBeforeMethodExample();
example.isAfterMethodExample();
example.getMethodExample();
example.fromMethodExample();
example.formatMethodExample();
example.atDayMethodExample();
}
/**
* Demonstration of Year.parse(CharSequence text)
*/
public void withMethodExample() {
Year date = Year.parse("2017");
Year result = date.with(Year.of(2010));
System.out.println(result);
}
/**
* Demonstration of Year.until(Temporal endExclusive, TemporalUnit unit)
*/
public void untilMethodExample() {
Year date = Year.parse("2015");
Year date1 = Year.now();
System.out.println(date.until(date1, ChronoUnit.YEARS));
}
/**
* Demonstration of Year.toString()
*/
public void toStringMethodExample() {
Year date = Year.parse("2017");
System.out.println(date.toString());
}
/**
* Demonstration of Year.range(TemporalField field)
*/
public void rangeMethodExample() {
Year date = Year.parse("2014");
System.out.println("Range : " + date.range(ChronoField.YEAR_OF_ERA));
}
/**
* Demonstration of Year.query(TemporalQuery<TemporalUnit> query)
*/
public void queryMethodExample() {
Year date = Year.parse("2014");
System.out.printf("Year precision is %s%n", date.query(TemporalQueries.precision()));
}
/**
* Demonstration of Year.plusYears(long yearsToAdd)
*/
public void plusYearsMethodExample() {
Year date = Year.parse("2017");
Year date1 = date.plusYears(10);
System.out.println(date1);
}
/**
* Demonstration of Year.of(int isoYear)
*/
public void ofMethodExample() {
Year year = Year.of(2017);
System.out.println(year);
}
/**
* Demonstration of Year.now(ZoneId zone)
*/
public void nowMethod() {
Year date = Year.now(ZoneId.systemDefault());
System.out.println(date);
}
/**
* Demonstration of Year.now(Clock clock)
*/
public void now() {
Year date = Year.now(Clock.systemUTC());
System.out.println(date);
}
/**
* Demonstration of Year.now()
*/
public void nowMethodExample() {
Year date = Year.now();
System.out.println(date);
}
/**
* Demonstration of Year.minusYears(long yearsToSubtract)
*/
public void minusYearsMethodExample() {
Year date = Year.of(2017);
System.out.println(date.get(ChronoField.YEAR));
System.out.println(date.minusYears(4).get(ChronoField.YEAR));
}
/**
* Demonstration of Year.minus(long amountToSubtract, TemporalUnit unit)
*/
public void minusMethodExample() {
Year date = Year.of(2017);
System.out.println(date.minus(2, ChronoUnit.YEARS));
}
/**
* Demonstration of Year.isLeap(long year)
*/
public void isLeapMethodExample() {
System.out.println(Year.isLeap(2016));
}
/**
* Demonstration of Year.isBefore(Year other)
*/
public void isBeforeMethodExample() {
Year date = Year.of(2016);
Year date1 = Year.of(2017);
System.out.println(date1.isBefore(date));
}
/**
* Demonstration of Year.isAfter(Year other)
*/
public void isAfterMethodExample() {
Year date = Year.of(2016);
Year date1 = Year.of(2017);
System.out.println(date1.isAfter(date));
}
/**
* Demonstration of Year.get(TemporalField field)
*/
public void getMethodExample() {
Year date = Year.of(2017);
System.out.println(date.get(ChronoField.YEAR_OF_ERA));
}
/**
* Demonstration of Year.from(TemporalAccessor temporal)
*/
public void fromMethodExample() {
Year date = Year.from(Year.of(2017));
System.out.println(date);
}
/**
* Demonstration of java.time.format.DateTimeFormatter.format(TemporalAccessor temporal)
*/
public void formatMethodExample() {
Year date = Year.of(2017);
System.out.println(date);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy");
System.out.println(formatter.format(date));
}
/**
* Demonstration of Year.atDay(int dayOfYear)
*/
public void atDayMethodExample() {
Year year = Year.of(2015);
LocalDate date = year.atDay(26);
System.out.println(date);
}
}
2010
3
2017
Range : 1 - 999999999
Year precision is Years
2027
2017
2018
2018
2018
2017
2013
2015
true
false
true
2017
2017
2017
17
2015-01-26
Comments
Post a Comment
Leave Comment