Java Program to Convert Date to String

In the previous article we have discussed Java Program to Convert char to String

In this article we will see how to convert a date to string.

Program To Convert Date To String

Before going into the program, let’s see some examples of both  String and date type.

Example-1: String types

S1= “2021-54-07 10:54:25”
Examples-2: Date  types 

LocalDate a = 1999-20-10;

Let’s see different ways to convert date to String.

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Method 1 : Java Program to Convert Date to String Using format() method

This is the method of DateFormat class which is used to convert the given Date into String .

Approach :

  1. Take the instant date and time through inbuild function getInstance().getTime() , store it in a variable lets say “d”.
  2. Define the format of date .
  3. Convert the date into the String according to the format  and store it into a variable output .
  4. Print the result .

Program :

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

public class Main

    public static void main(String args[])
    // taking the instant date and time
    Date d = Calendar.getInstance().getTime();
    // converting to the date format
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    String output = df.format(d);
    System.out.println("Converted date String: " + output);
Output : 

Converted date String: 09/08/2021 02:43:018

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Program: