Year format() method in Java with Examples Last Updated : 27 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The format() method of Year class in Java is used to format the current Year object according to the DateTimeFormatter passed to it as a parameter. Syntax: public String format(DateTimeFormatter formatter) Parameter: This method accepts a single parameter formatter. It specifies a DateTimeFormatter according to which the current Year object will be formatted. It can not be NULL. Return Value: It returns a string which is the formatted Year value. Exception: This method throws a DateTimeException if any error occurs during formatting of the year object. Below programs illustrate the format() method of Year in Java: Program 1: Java // Program to illustrate the format() method import java.util.*; import java.time.*; import java.time.format.DateTimeFormatter; public class GfG { public static void main(String[] args) { // Creates a Year object Year firstYear = Year.of(1997); // Print the current year in // default format System.out.println(firstYear); // Create a DateTimeFormatter DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy"); // Print the current year after formatting System.out.println(firstYear.format(formatter)); } } Output: 1997 97 Program 2: Java // Program to illustrate the format() method import java.util.*; import java.time.*; import java.time.format.DateTimeFormatter; public class GfG { public static void main(String[] args) { // Creates a Year object Year firstYear = Year.of(2018); // Print the current year in // default format System.out.println(firstYear); // Create a DateTimeFormatter DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy"); // Print the current year after formatting System.out.println(firstYear.format(formatter)); } } Output: 2018 18 Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#format-java.time.format.DateTimeFormatter- Comment More infoAdvertise with us Next Article Year of() method in Java with Examples B barykrg Follow Improve Article Tags : Misc Java Java-Functions Java-time package Java-Year +1 More Practice Tags : JavaMisc Similar Reads Year from() Method in Java with Examples from() method of the Year class used to get instance of Year from TemporalAccessor object passed as parameter. A TemporalAccessor represents an arbitrary set of date and time information and this method helps to get a year object based on the specified TemporalAccessor object. Syntax: public static 2 min read Year get() method in Java with Examples get() method of the Year class used to get the value for the specified field passed as parameter from this Year as an integer value. This method queries this year for the value of the field and the returned value will always be within the valid range of values for the field. When the field is not su 2 min read Year of() method in Java with Examples The of() method of Year class in Java is used to return an Year instance. It accepts an year according to the proleptic ISO calendar system and returns an instance of Year with the specified isoYear. Syntax: public static Year of(int isoYear) Parameter: It accepts a single integral value isoYear as 1 min read Year now() method in Java with examples The now() method of Year class in Java is used to return the current year from the system clock in the default time-zone. Syntax: public static Year now() or, public static Year now(Clock clock) or, public static Year now(ZoneId zone) Parameter: The parameter is optional for this method as shown in 2 min read Year getValue() method in Java with Examples The getValue() method of Year class in Java is used to get the integral value of the current Year object. Syntax: public int getValue() Parameter: This method does not accepts any parameter. Return Value: It returns an integer denoting the value of the current year object. Below programs illustrate 1 min read YearMonth get() method in Java with Examples The get() method of the YearMonth class in Java is used to get the value of the specified field from this year-month as an integer value. This method queries this year-month for the value of the specified field. The returned value will always be within the valid range. An exception is thrown if it i 2 min read Like