Date and Time

How to Convert Date object to String in Java

Table of Contents

Program

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateToString 
{
	public static void main(String[] args) 
	{
		Date date = Calendar.getInstance().getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
		String strD = sdf.format(date);
		System.out.println("Date to String Is : "+ strD);
	}

}

Output

Date to String Is : 2021-01-08 05:01:47

Description

public final String format​(Date date)

Formats a Date into a date-time string.

Parameters:

date – the time value to be formatted into a date-time string.

Returns:

the formatted date-time string.