Friday, October 10, 2025
HomeLanguagesJavaDate toString() method in Java with Examples

Date toString() method in Java with Examples

The toString() method of Java Date class converts this date object into a String in form “dow mon dd hh:mm:ss zzz yyy”. This method overrides toString in class object.
Syntax:

public String toString()

Parameters: The function does not accept any parameter.

Return Value: It method returns a string representation of this date.

Exception: The function does not throws any exception.

Program below demonstrates the above mentioned function:




// Java code to demonstrate
// toString() function of Date class
  
import java.util.Date;
import java.util.Calendar;
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // creating a Calendar object
        Calendar c1 = Calendar.getInstance();
  
        // set Month
        // MONTH starts with 0 i.e. ( 0 - Jan)
        c1.set(Calendar.MONTH, 11);
  
        // set Date
        c1.set(Calendar.DATE, 05);
  
        // set Year
        c1.set(Calendar.YEAR, 1996);
  
        // creating a date object with specified time.
        Date dateOne = c1.getTime();
  
        System.out.println(dateOne.toString());
    }
}


Output:

Thu Dec 05 08:21:00 UTC 1996




// Java code to demonstrate
// toString() function of Date class
  
import java.util.Date;
import java.util.Calendar;
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // creating a Calendar object
        Calendar c1 = Calendar.getInstance();
  
        // set Month
        // MONTH starts with 0 i.e. ( 0 - Jan)
        c1.set(Calendar.MONTH, 11);
  
        // set Date
        c1.set(Calendar.DATE, 15);
  
        // set Year
        c1.set(Calendar.YEAR, 1999);
  
        // creating a date object with specified time.
        Date dateOne = c1.getTime();
  
        System.out.println(dateOne.toString());
    }
}


Output:

Wed Dec 15 08:21:02 UTC 1999
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS