Java Convert Date to String
In this Java core tutorial we learn how to convert a java.util.Date object into date and time format String in Java programming language.
Table of contents
- Format Date object to date time String
- Convert Date to String format dd/MM/yyyy in Java
- Convert Date to String using toString() method
Format Date object to date time String
To format a Date object as date time String we can use the SimpleDateFormat class with specified format pattern as following Java program.
ConvertDateToStringExample1.java
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConvertDateToStringExample1 {
public static void main(String... args) {
Date date = new Date();
// Convert Date to String
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String dateString = simpleDateFormat.format(date);
System.out.println(dateString);
}
}
2022/04/16 16:46:23
Convert Date to String format dd/MM/yyyy in Java
The following Java program to show for more example of format a Date object to String in format dd/MM/yyyy using the SimpleDateFormat class.
ConvertDateToStringExample3.java
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConvertDateToStringExample3 {
public static void main(String... args) {
Date date = new Date();
// Convert Date to String
String formatPattern = "dd/MM/yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatPattern);
String dateString = simpleDateFormat.format(date);
System.out.println("Date: " + date);
System.out.println("Date in dd/MM/yyyy: " + dateString);
}
}
Date: Sat Apr 16 17:04:33 ICT 2022
Date in dd/MM/yyyy: 16/04/2022
Convert Date to String using toString() method
In Java we also can simply use the Date.toString() method to get String value of a given Date object as the Java program below.
ConvertDateToStringExample2.java
import java.util.Date;
public class ConvertDateToStringExample2 {
public static void main(String... args) {
Date date = new Date();
// Convert Date to String
String dateString = date.toString();
System.out.println(dateString);
}
}
Sat Apr 16 16:46:57 ICT 2022
Happy Coding 😊