×

How to Convert String to Date in Java

How to Convert String to Date in java

You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes. Both classes belong to java.text package. The SimpleDateFormat class is used to both parse and format dates according to the formatting pattern specified by you.

You can create a SimpleDateFormat instance like this:

String pattern = "yyyy-MM-dd";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

The pattern parameter passed to the constructor of the SimpleDateFormat class. The pattern is used for formatting and parsing of dates.

Remember

There is a little bit confusion between “m” and “M”, “m” represents a minutes while “M” represents the Month. Similarly “d” represents a date in the month while “D” represents Day of the week. This is the most common cause of error while converting String to Date.  In short, ddMMyy is not equal to DDmmyy.

Example

In this example, Datestr is a variable of String type contains a string “23/05/2019”. The date is a variable of type Date stores the converted value of Datestr. A new is a keyword that creates the constructor of the SimpleDateFormat class and parses the date format as an argument. parse() is a method of SimpleDateFormat class and parses Datestr as an argument which is to be converted into Date. The println statement prints the converted value of Datestr.

The parse() method throws an exception java.text.ParseException, to remove this exception you should use either try catch block or simply write throws Exception after the main method.

import java.text.SimpleDateFormat; 
import java.util.Date; 
public class StringtoDateExample
{ 
public static void main(String[] args)throws Exception
{ 
String Datestr="23/05/2019"; 
Date date=new SimpleDateFormat("dd/MM/yyyy").parse(Datestr); 
System.out.println(date); 
} 
}

Output

Thu May 23 00:00:00 IST 2019

Let’s see another example in which different date format is used.

Example

import java.text.SimpleDateFormat;  
import java.util.Date;  
public class StringToDateExample2 
{  
public static void main(String[] args)throws Exception 
{  
String Datefrmt1="31/12/1998";  
String Datefrmt2= "23-May-2019";  
String Datefrmt3= "05 23, 2019";  
String Datefrmt4= "Thu, May 23 2019";  
String Datefrmt5 = "Thu, May 23 2019 21:31:20";  
String Datefrmt6 = "23-May-2019 21:31:20";  
SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");  
SimpleDateFormat formatter2=new SimpleDateFormat("dd-MMM-yyyy");  
SimpleDateFormat formatter3=new SimpleDateFormat("MM dd, yyyy");  
SimpleDateFormat formatter4=new SimpleDateFormat("E, MMM dd yyyy");  
SimpleDateFormat formatter5=new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");  
SimpleDateFormat formatter6=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");  
Date date1=formatter1.parse(Datefrmt1);  
Date date2=formatter2.parse(Datefrmt2);  
Date date3=formatter3.parse(Datefrmt3);  
Date date4=formatter4.parse(Datefrmt4);  
Date date5=formatter5.parse(Datefrmt5);  
Date date6=formatter6.parse(Datefrmt6);  
System.out.println(Datefrmt1+"\t"+date1);  
System.out.println(Datefrmt2+"\t"+date2);  
System.out.println(Datefrmt3+"\t"+date3);  
System.out.println(Datefrmt4+"\t"+date4);  
System.out.println(Datefrmt5+"\t"+date5);  
System.out.println(Datefrmt6+"\t"+date6);  
}  
}

Output

23/05/2019 Thu May 23 00:00:00 IST 2019
23-May-2019 Thu May 23 00:00:00 IST 2019
05 23, 2019 Thu May 23 00:00:00 IST 2019
Thu, May 23 2019 Thu May 23 00:00:00 IST 2019
Thu, May 23 2019 21:31:20 Thu May 23 21:31:20 IST 2019
23-May-2019 21:31:20 Thu May 23 21:31:20 IST 2019

Related Topics

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

Java For Loop

A for loop is used to execute a set of statements for a fixed number of times. It takes the following form: for (initialization; condition; update) { statements; } The for loop defines...

2 minutes read.

Time Class Operation in Java

The Java SQL package includes the time class. This class is merely a lightweight wrapper for java. util. THANKS TO THE recognize BC API can recognize this as a SQL...

3 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

How to Convert Octal to Decimal in Java

How to Convert Octal to Decimal in Java There are two methods to convert Octal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method The Integer.parseInt() method is a static method...

2 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Collection Interfaces in Java with Examples

In this tutorial, we will discuss collection interfaces in Java with Examples. Introduction The Collection Interface is an individual from the Java Collections Framework. It is a root point of interaction of...

4 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Bully Algorithm code in Java

Election algorithms include the bully algorithm, mainly used to select a coordinate. To find a coordinator in a distributed system that can carry out the tasks required by other processes,...

4 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.