Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Convert String to Date in Java

A string can store dates, but it cannot be used to perform common date operations, like extracting the month of the year from the date. In this tutorial, we will learn how to convert a string to a date in Java so that date-related operations can be applied to it.

Converting String to LocalDate and LocalDateTime

String objects can be easily converted to objects of LocalDate and LocalDateTime classes by using the parse() method. Make sure that the string contains a valid format of date and time otherwise, we may get a DateTimeParseException.

public static void main(String[] args)
{		
	//LocalDate
	String strDate = "2021-07-23";
	LocalDate date = LocalDate.parse(strDate);
	System.out.println(date);
		
	//LocalDateTime
	String strDateTime = "2021-07-23T10:30:59";
	LocalDateTime dateTime = LocalDateTime.parse(strDateTime);
	System.out.println(dateTime);
}


2021-07-23
2021-07-23T10:30:59

A drawback of this approach is that if the string contains some other date format that is not recognized by the parse() method, then we will get a DateTimeParseException. This scenario is demonstrated by the code below.

public static void main(String[] args)
{
	String strDate = "2021-July-23";
	LocalDate date = LocalDate.parse(strDate);
	System.out.println(date);

}


Exception in thread "main" java.time.format.DateTimeParseException: Text '2021-July-23' could not be parsed at index 5

Converting String to Date using Custom Formatters

Sometimes the string may contain a date in some other format and the parse() method may not recognize this format. In such cases, we can use custom formatters. Java has a DateTimeFormatter class and a SimpleDateFormat class, which can be used to specify a format for our string. We can then use the parse() method of these classes to parse the string and convert it to an appropriate date.

The list below shows some of the most commonly used date and time patterns for formatting strings.

  • y - Year like 2021, or 21
  • M - Month of the year like July, or 07
  • d - Day of the month
  • E - Day name like Friday, or Fri
  • a - AM/PM marker
  • H - Hour(0-23)
  • h - Hour in AM and PM(0-12)
  • m - Minutes(0-59)
  • s - Seconds(0-59)

A few examples of the DateTimeFormatter class are shown below.

Date of Format- YYYY-MM-DD

Let's try to convert a simple date format from String to LocalDate. We will use yyyy for the year, a single M for the month, and a single d for the day of the month.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class demo
{
	public static void main(String[] args)
	{
		String strDate1 = "2021-07-23";
		DateTimeFormatter f1 = DateTimeFormatter.ofPattern("yyyy-M-d");
		LocalDate d1 = LocalDate.parse(strDate1, f1);
		System.out.println(d1);
	}
}


2021-07-23

Date of Format- Day name, DD Month Name, YYYY

Now, we also have the day name included in the date. To parse this string, we will use EEEE for the full day name. We will use d for the date, M for the month, and yyyy for the year.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class demo
{
	public static void main(String[] args)
	{
		String strDate1 = "Friday, 23/07/2021";
		DateTimeFormatter f1 = DateTimeFormatter.ofPattern("EEEE, d/M/yyyy");
		LocalDate d1 = LocalDate.parse(strDate1, f1);
		System.out.println(d1);
	}
}

2021-07-23

Date of Format- Three letter day name, DD Month Name, YYYY

This time we have a three-letter day name and the full month name in the string. We will use EEE for the day name, and MMMM for the month name. The letter d is used for date and yyyy is used for the year.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class demo
{
	public static void main(String[] args)
	{
		String strDate1 = "Fri, 23 July, 2021";
		DateTimeFormatter f1 = DateTimeFormatter.ofPattern("EEE, d MMMM, yyyy");
		LocalDate d1 = LocalDate.parse(strDate1, f1);
		System.out.println(d1);
	}
}

2021-07-23

Date and Time of Format- DD-MM-YYYY HH:MM:SS AM/PM

We also have the time included in the string. For the hour part of the time, we will use HH. For minutes, we will use mm, and for seconds we will use ss. We will also use the letter a for AM and PM.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class demo
{
	public static void main(String[] args)
	{
		String strDate1 = "23-07-2021 10:30:59 AM";
		DateTimeFormatter f1 = DateTimeFormatter.ofPattern("d-M-yyyy HH:mm:ss a");
		LocalDate d1 = LocalDate.parse(strDate1, f1);
		System.out.println(d1);
	}
}

2021-07-23

The working of the SimpleDateFormat class is shown below. We need to enclose our code in a try-catch block to avoid the ParseException.

Date of Format- YYYY-MM-DD

We will use yyyy for the year, M for the month, and a single d to denote the day of the month.

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

public class Demo
{
	public static void main(String[] args)
	{
		String strDate1 = "2021-07-23";
		SimpleDateFormat f1 = new SimpleDateFormat("yyyy-M-d");
		try
		{
			Date d1 = f1.parse(strDate1);
			System.out.println(d1);
		}
		catch(ParseException e)
		{
			System.out.println(e);
		}
	}
}


Fri Jul 23 00:00:00 IST 2021

Date of Format-Full Day Name, DD/MM/YYYY

We will use EEEE for the full day name to parse the string with the day name. We will use d for the date, M for the month, and yyyy for the year.

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

public class Demo
{
	public static void main(String[] args)
	{
		String strDate1 = "Friday, 23/07/2021";
		SimpleDateFormat f1 = new SimpleDateFormat("EEEE, d/M/yyyy");
		try
		{
			Date d1 = f1.parse(strDate1);
			System.out.println(d1);
		}
		catch(ParseException e)
		{
			System.out.println(e);
		}
	}
}

Fri Jul 23 00:00:00 IST 2021

Date of Format- Three letter day name, DD Month Name, YYYY

Three letter day-name can be represented using EEE. We will use MMM for the full month name.

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

public class Demo
{
	public static void main(String[] args)
	{
		String strDate1 = "Fri, 23 July, 2021";
		SimpleDateFormat f1 = new SimpleDateFormat("EEE, d MMM, yyyy");
		try
		{
			Date d1 = f1.parse(strDate1);
			System.out.println(d1);
		}
		catch(ParseException e)
		{
			System.out.println(e);
		}
	}
}

Fri Jul 23 00:00:00 IST 2021

Date and Time of Format- DD-MM-YYYY HH:MM:SS AM/PM

For the hour part of the time, we will use HH. Use mm for minutes, and for seconds we will use ss. We will also use the letter a for AM and PM.

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

public class Demo
{
	public static void main(String[] args)
	{
		String strDate1 = "23-07-2021 10:30:59 AM";
		SimpleDateFormat f1 = new SimpleDateFormat("d-M-yyyy HH:mm:ss a");
		try
		{
			Date d1 = f1.parse(strDate1);
			System.out.println(d1);
		}
		catch(ParseException e)
		{
			System.out.println(e);
		}
	}
}

Fri Jul 23 00:00:00 IST 2021

Working with Timezones

The character Z in a date string is used to represent the timezone. By default, the SimpleDateFormat class will display the timezone that we had set for the JVM. We can use the setTimeZone() method of the SimpleDateFormat class to view a different time. This time is then converted to the JVM timezone.

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

public class demo
{
	public static void main(String[] args)
	{
		String strDate1 = "23-07-2021 10:30:59Z";
		
		SimpleDateFormat f1 = new SimpleDateFormat("d-M-yyyy HH:mm:ss");
		SimpleDateFormat f2 = new SimpleDateFormat("d-M-yyyy HH:mm:ss");
		f2.setTimeZone(TimeZone.getTimeZone("GMT"));
		try
		{
			Date d1 = f1.parse(strDate1);
			Date d2 = f2.parse(strDate1);
			System.out.println(d1);
			System.out.println(d2);
		}
		catch(ParseException e)
		{
			System.out.println(e);
		}
	}
}


Fri Jul 23 10:30:59 IST 2021
Fri Jul 23 16:00:59 IST 2021

We can also use ZonedDateTime to deal with timezones.

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class demo
{
	public static void main(String[] args)
	{
		DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
		ZonedDateTime z1 = ZonedDateTime.parse("2021-07-23 10:30:59 Europe/London", f);
		ZonedDateTime z2 = ZonedDateTime.parse("2021-07-23 10:30:59 Europe/Madrid", f);
		System.out.println(z1);
		System.out.println(z2);
	}
}


2021-07-23T10:30:59+01:00[Europe/London]
2021-07-23T10:30:59+02:00[Europe/Madrid]

Using Apache Commons Lang3

The Apache Commons is a well-known library and it provides a DateUtils class to work with dates. We can convert a string to a date by using the parseDate() method and passing an array of date formats.

import java.text.ParseException;
import java.util.Date;
import org.apache.commons.lang3.time.DateUtils;

public class StringToDate
{
	public static void main(String[] args)
	{
		try
		{
			String dateInString = "2021-07-23";
            Date date = DateUtils.parseDate(dateInString, new String[] {"yyyy-MM-dd"});
            System.out.println(date);   
		}
		catch (ParseException e)
		{
			System.out.print(e);
		}
	}

}


Fri Jul 23 00:00:00 IST 2021

Summary

This tutorial explains how we can convert a string to a date. Strings are work fine if we just want to store dates and print them, but they do not allow us to perform other operations of the date value. We can convert a string containing a date in a standard format by using the parse() methods of the LocalDate and LocalDateTime classes. If the string is some other date format, then we can define a custom formatter using DateTimeFormatter class or the SimpleDateFormat class.