Java – Date to String conversion in different formats

In this article, we will learn how to convert Date to String in Java using different date formats

1. What is the need of converting Date to String ?

  • Generally, whenever we receive or send data from web application then it all passed in the form of String only
  • For displaying or transfer over the wire or network, Date has to be converted into String
  • This article explains about Date to String conversion in different formats
  • Note: Likewise, sometimes String to Date conversion is also required for reverse process

2. Date to String conversion in different formats:

We will use below formats to convert Date to String

  1. dd/MM/yyyy
  2. dd-MM-yyyy
  3. dd.MM.yyyy
  4. dd MM yyyy
  5. MM dd, yyyy
  6. dd-MMM-yyyy
  7. E, MMM dd yyyy
  8. E, MMM dd yyyy HH:mm:ss
  9. E, MMM dd yyyy hh:mm:ss a

Note: for creating above formats we need mainly 2 classes

  1. java.text.DateFormat
  2. java.text.SimpleDateFormat

3. Date to String conversion examples

3.1 Convert Date to String in (dd/MM/yyyy) format

DateToStringConversion1.java

package in.bench.resources.date.to.string.conversion;

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

public class DateToStringConversion1 {

	public static void main(String[] args) throws ParseException {

		// get current date
		Date date = new Date();


		// formatter
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");


		// format date using above formatter to convert into String
		String strDate = formatter.format(date);


		// print to console
		System.out.println("Original Date :- " + date);
		System.out.println("\nAfter formatting Date to String :- " + strDate);
	}
}

Output:

Original Date :- Wed Apr 20 15:23:58 IST 2022

After formatting Date to String :- 20/04/2022

3.2 Convert Date to String in (dd-MM-yyyy) format

DateToStringConversion2.java

package in.bench.resources.date.to.string.conversion;

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

public class DateToStringConversion2 {

	public static void main(String[] args) throws ParseException {

		// get current date
		Date date = new Date();


		// formatter
		DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");


		// format date using above formatter to convert into String
		String strDate = formatter.format(date);


		// print to console
		System.out.println("Original Date :- " + date);
		System.out.println("\nAfter formatting Date to String :- " + strDate);
	}
}

Output:

Original Date :- Wed Apr 20 15:24:20 IST 2022

After formatting Date to String :- 20-04-2022

3.3 Convert Date to String in (dd MM yyyy) format

DateToStringConversion3.java

package in.bench.resources.date.to.string.conversion;

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

public class DateToStringConversion3 {

	public static void main(String[] args) throws ParseException {

		// get current date
		Date date = new Date();


		// formatter
		DateFormat formatter = new SimpleDateFormat("dd MM yyyy");


		// format date using above formatter to convert into String
		String strDate = formatter.format(date);


		// print to console
		System.out.println("Original Date :- " + date);
		System.out.println("\nAfter formatting Date to String :- " + strDate);
	}
}

Output:

Original Date :- Wed Apr 20 15:24:40 IST 2022

After formatting Date to String :- 20 04 2022

3.4 Convert Date to String in (dd.MM.yyyy) format

DateToStringConversion4.java

package in.bench.resources.date.to.string.conversion;

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

public class DateToStringConversion4 {

	public static void main(String[] args) throws ParseException {

		// get current date
		Date date = new Date();


		// formatter
		DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");


		// format date using above formatter to convert into String
		String strDate = formatter.format(date);


		// print to console
		System.out.println("Original Date :- " + date);
		System.out.println("\nAfter formatting Date to String :- " + strDate);
	}
}

Output:

Original Date :- Wed Apr 20 15:25:01 IST 2022

After formatting Date to String :- 20.04.2022

3.5 Convert Date to String in (MM dd, yyyy) format

DateToStringConversion5.java

package in.bench.resources.date.to.string.conversion;

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

public class DateToStringConversion5 {

	public static void main(String[] args) throws ParseException {

		// get current date
		Date date = new Date();


		// formatter
		DateFormat formatter = new SimpleDateFormat("MM dd, yyyy");


		// format date using above formatter to convert into String
		String strDate = formatter.format(date);


		// print to console
		System.out.println("Original Date :- " + date);
		System.out.println("\nAfter formatting Date to String :- " + strDate);
	}
}

Output:

Original Date :- Wed Apr 20 15:25:28 IST 2022

After formatting Date to String :- 04 20, 2022

3.6 Convert Date to String in (dd-MMM-yyyy) format

DateToStringConversion6.java

package in.bench.resources.date.to.string.conversion;

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

public class DateToStringConversion6 {

	public static void main(String[] args) throws ParseException {

		// get current date
		Date date = new Date();


		// formatter
		DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");


		// format date using above formatter to convert into String
		String strDate = formatter.format(date);


		// print to console
		System.out.println("Original Date :- " + date);
		System.out.println("\nAfter formatting Date to String :- " + strDate);
	}
}

Output:

Original Date :- Wed Apr 20 15:25:47 IST 2022

After formatting Date to String :- 20-Apr-2022

3.7 Convert Date to String in (E, MMM dd yyyy) format

DateToStringConversion7.java

package in.bench.resources.date.to.string.conversion;

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

public class DateToStringConversion7 {

	public static void main(String[] args) throws ParseException {

		// get current date
		Date date = new Date();


		// formatter
		DateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");


		// format date using above formatter to convert into String
		String strDate = formatter.format(date);


		// print to console
		System.out.println("Original Date :- " + date);
		System.out.println("\nAfter formatting Date to String :- " + strDate);
	}
}

Output:

Original Date :- Wed Apr 20 15:26:09 IST 2022

After formatting Date to String :- Wed, Apr 20 2022

3.8 Convert Date to String in (E, MMM dd yyyy HH:mm:ss) format

DateToStringConversion8.java

package in.bench.resources.date.to.string.conversion;

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

public class DateToStringConversion8 {

	public static void main(String[] args) throws ParseException {

		// get current date
		Date date = new Date();


		// formatter
		DateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");


		// format date using above formatter to convert into String
		String strDate = formatter.format(date);


		// print to console
		System.out.println("Original Date :- " + date);
		System.out.println("\nAfter formatting Date to String :- " + strDate);
	}
}

Output:

Original Date :- Wed Apr 20 15:26:31 IST 2022

After formatting Date to String :- Wed, Apr 20 2022 15:26:31

3.9 Convert Date to String in (E, MMM dd yyyy hh:mm:ss a) format

DateToStringConversion9.java

package in.bench.resources.date.to.string.conversion;

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

public class DateToStringConversion9 {

	public static void main(String[] args) throws ParseException {

		// get current date
		Date date = new Date();


		// formatter
		DateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy hh:mm:ss a");


		// format date using above formatter to convert into String
		String strDate = formatter.format(date);


		// print to console
		System.out.println("Original Date :- " + date);
		System.out.println("\nAfter formatting Date to String :- " + strDate);
	}
}

Output:

Original Date :- Wed Apr 20 15:26:50 IST 2022

After formatting Date to String :- Wed, Apr 20 2022 03:26:50 PM

Hope, everyone found this article useful while converting Date to String. If any edge cases needs to be covered then comment

Happy Coding !!
Happy Learning !!

Java 8 - How to split a String and Collect to any Collection ?
Java - String to Date conversion in different formats