Date toString() method in Java with Examples
Last Updated :
02 Jan, 2019
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:
import java.util.Date;
import java.util.Calendar;
public class GfG {
public static void main(String[] args)
{
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.MONTH, 11 );
c1.set(Calendar.DATE, 05 );
c1.set(Calendar.YEAR, 1996 );
Date dateOne = c1.getTime();
System.out.println(dateOne.toString());
}
}
|
Output:
Thu Dec 05 08:21:00 UTC 1996
import java.util.Date;
import java.util.Calendar;
public class GfG {
public static void main(String[] args)
{
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.MONTH, 11 );
c1.set(Calendar.DATE, 15 );
c1.set(Calendar.YEAR, 1999 );
Date dateOne = c1.getTime();
System.out.println(dateOne.toString());
}
}
|
Output:
Wed Dec 15 08:21:02 UTC 1999
Share your thoughts in the comments
Please Login to comment...