Skip to content
Related Articles

Related Articles

Year toString() method in Java with Examples
  • Last Updated : 27 Nov, 2018

The toString() method of Year class in Java is used to return the string representation of this Year object.

Syntax:

public String toString()

Parameter: This method does not accepts any parameter.

Return Value: It returns the string representation of this Year object.

Below programs illustrate the toString() method of Year in Java:



Program 1:




// Program to illustrate the toString() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Create a valid Year object
        Year thisYear = Year.of(2018);
  
        // Print the string representation
        System.out.println(thisYear.toString());
    }
}


Output:

2018

Program 2:




// Program to illustrate the toString() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Create a valid Year object
        Year thisYear = Year.of(1997);
  
        // Print the string representation
        System.out.println(thisYear.toString());
    }
}


Output:

1997

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#toString–

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :