Java convert date to string

Let’s learn java convert date to string.

Java convert date to string

To convert date to string, format() method is used of class DateFormat.

Here DateFormat class is an abstract class and it’s child class is SimpleDateFormat class.

The syntax and example for format() method of DateFormat class is as below.

Syntax:

String format(Date d)

Example:

Date dt = Calendar.getInstance().getTime();

DateFormat df = new SimpleDateFormat(“yyyy-mm-dd hh:mm:ss”);

Also read – arrays in java

String strDate = df.format(dt); // 2015-24-08 02:40:27


Let’s see java date to string example.

import java.util.Date;
import java.text.SimpleDateFormat;
public class DateToStringDemo
{
   public static void main(String[] args)
   {
      Date dt = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
      String strDate = sdf.format(dt);
      System.out.println("Date format - MM/dd/yyyy to string is : " + strDate);
      sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
      strDate = sdf.format(dt);
      System.out.println("Date format - dd-M-yyyy hh:mm:ss to string is : " + strDate);
      sdf = new SimpleDateFormat("dd MMMM yyyy");
      strDate = sdf.format(dt);
      System.out.println("Date format - dd MMMM yyyy to string is : " + strDate);
      sdf = new SimpleDateFormat("dd MMMM yyyy zzzz");
      strDate = sdf.format(dt);
      System.out.println("Date format - dd MMMM yyyy zzzz to string is : " + strDate);
      sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
      strDate = sdf.format(dt);
      System.out.println("Date format - E, dd MMM yyyy HH:mm:ss z to string is : " + strDate);
   }
}


Output:

Date format – MM/dd/yyyy to string is : 12/18/2018

Date format – dd-M-yyyy hh:mm:ss to string is : 18-12-2018 02:20:35

Date format – dd MMMM yyyy to string is : 18 December 2018

Date format – dd MMMM yyyy zzzz to string is : 18 December 2018 India Standard Time

Date format – E, dd MMM yyyy HH:mm:ss z to string is : Tue, 18 Dec 2018 14:20:35 IST


convert date to string java 8

To convert date to string use DateTimeFormatter and LocalDateTime class.

Let’s see an example on date to string java 8.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class DateToStringJava8
{
   public static void main(String[] args) throws ParseException
   {
      String strInput = "2005-05-14 05:05:14";
      SimpleDateFormat before = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      Date date = before.parse(strInput);
      SimpleDateFormat after = new SimpleDateFormat("yyyy-MM-dd");
      String strOutput = after.format(date);
      DateTimeFormatter dtfOld = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      DateTimeFormatter dtfNew = DateTimeFormatter.ofPattern("yyyy-MM-dd");
      LocalDateTime ldt = LocalDateTime.parse(strInput, dtfOld);
      strOutput = ldt.format(dtfNew);
      System.out.println("Before - date to string java 8: " + strInput);
      System.out.println("Now - date to string java 8: " + strOutput);
   }
}


Output:

Before – date to string java 8: 2005-05-14 05:05:14
Now – date to string java 8: 2005-05-14


This is all about convert date to string. I hope you have understood the concept of converting date to string in java.