LAST UPDATED: NOVEMBER 27, 2020
Java LocalDate toString() Method
This method is used to get String
representation of this date. It returns this date as a String
, such as 2017-12-03.
It is helpful if we want to convert LocalDate
object to String object. The output will be in the ISO-8601 format uuuu-MM-dd
.
It does not take any argument but returns a String
. The syntax of the method is given below.
Syntax
public String toString()
Parameters:
It does not take any parameter.
Returns:
A string representation of this date.
Time for an Example:
Let's take an example to get a string representation of the localdate. Here, we have an object of localdate class and getting string by using the toString()
method.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.of(2020, 01, 10);
System.out.println(localDate);
String date = localDate.toString();
System.out.println("String date : "+date);
}
}
1970-01-10
Epoch Days : 9
Time for another Example:
Let's take another example to understand the toString()
method. Here, we are using toString()
method to get String representation of localdate object.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
String date = localDate.toString();
System.out.println("String date : "+date);
}
}
2020-06-19
String date : 2020-06-19
Live Example:
Try with a live example, execute the code instantly with our powerful Online Java Compiler.