Java – String to Date conversion in different formats

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

Q) What is the need of converting String to Date ?

  • Generally, whenever we receive any data from web application then it is passed in the form of String only
  • But for further processing we need to convert String to Date
  • This article explains about String to Date conversion in different formats
  • Note: Likewise, sometimes Date to String conversion is also required for reverse process

1. String to Date conversion in different formats:

We will use below formats to convert String to Date

  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

2. String to Date conversion examples :

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

StringToDateFormat1.java

package in.bench.resources.date.conversion;

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

public class StringToDateFormat1 {

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

		// date in String
		String strDate = "23/04/2021";


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


		// parse date in String format using above formatter
		Date date = dateFormat.parse(strDate);


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

Output:

Original String :- 23/04/2021

Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021

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

StringToDateFormat2.java

package in.bench.resources.date.conversion;

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

public class StringToDateFormat2 {

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

		// date in String
		String strDate = "23-04-2021";


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


		// parse date in String format using above formatter
		Date date = dateFormat.parse(strDate);


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

Output:

Original String :- 23-04-2021

Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021

2.3 Convert String to Date in (dd MM yyyy) format :

StringToDateFormat3.java

package in.bench.resources.date.conversion;

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

public class StringToDateFormat3 {

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

		// date in String
		String strDate = "23 04 2021";


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


		// parse date in String format using above formatter
		Date date = dateFormat.parse(strDate);


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

Output:

Original String :- 23 04 2021

Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021

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

StringToDateFormat4.java

package in.bench.resources.date.conversion;

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

public class StringToDateFormat4 {

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

		// date in String
		String strDate = "23.04.2021";


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


		// parse date in String format using above formatter
		Date date = dateFormat.parse(strDate);


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

Output:

Original String :- 23.04.2021

Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021

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

StringToDateFormat5.java

package in.bench.resources.date.conversion;

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

public class StringToDateFormat5 {

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

		// date in String
		String strDate = "04 23, 2021";


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


		// parse date in String format using above formatter
		Date date = dateFormat.parse(strDate);


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

Output:

Original String :- 04 23, 2021

Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021

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

StringToDateFormat6.java

package in.bench.resources.date.conversion;

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

public class StringToDateFormat6 {

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

		// date in String
		String strDate = "23-Apr-2021";


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


		// parse date in String format using above formatter
		Date date = dateFormat.parse(strDate);


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

Output:

Original String :- 23-Apr-2021

Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021

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

StringToDateFormat7.java

package in.bench.resources.date.conversion;

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

public class StringToDateFormat7 {

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

		// date in String
		String strDate = "Fri, Apr 23 2021";


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


		// parse date in String format using above formatter
		Date date = dateFormat.parse(strDate);


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

Output:

Original String :- Fri, Apr 23 2021

Converted Date after formatting :- Fri Apr 23 00:00:00 IST 2021

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

StringToDateFormat8.java

package in.bench.resources.date.conversion;

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

public class StringToDateFormat8 {

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

		// date in String
		String strDate = "Fri, Apr 23 2021 14:20:27";


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


		// parse date in String format using above formatter
		Date date = dateFormat.parse(strDate);


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

Output:

Original String :- Fri, Apr 23 2021 14:20:27

Converted Date after formatting :- Fri Apr 23 14:20:27 IST 2021

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

StringToDateFormat9.java

package in.bench.resources.date.conversion;

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

public class StringToDateFormat9 {

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

		// date in String
		String strDate = "Fri, Apr 23 2021 02:21:59 PM";


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


		// parse date in String format using above formatter
		Date date = dateFormat.parse(strDate);


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

Output:

Original String :- Fri, Apr 23 2021 02:21:59 PM

Converted Date after formatting :- Fri Apr 23 14:21:59 IST 2021

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

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Date to String conversion in different formats
Java - How to remove white-spaces in a String ?