Wednesday, September 3, 2025
HomeLanguagesJavaYear toString() method in Java with Examples

Year toString() method in Java with Examples

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–

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS